Table of contents
1.
Introduction
2.
DescendingIterator()
2.1.
Example 1
2.2.
Example 2
3.
Frequently Asked Questions
3.1.
What is a linked list in C++?
3.2.
What are the applications of a linked list?
3.3.
How do you delete a node in a linked list?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

LinkedList descendingIterator in Java

Author Saksham Gupta
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?
Linked List

Introduction

Linked List is always on the mind of interviewers when it comes to coding interviews. Having a good grasp of the interviewer’s favorite topic surely gives us an upper hand. Instead of traditional problem-solving today, we will see an iterator that is provided by Java for linked lists, i.e., LinkedList descendingIterator.

But what is an iterator?

In simple words, an Iterator is an object that is used to loop through different collections, like ArrayList, HashSet, and Linked List.

Let’s now talk about the LinkedList descendingIterator.

DescendingIterator()

Like any other iterator, the descendingIterator() method returns an iterator to loop through the elements of the LinkedList, but the difference here is it returns the elements in reverse order. In other words, we can say the elements will be returned from tail to head, and the iterator would be pointing towards the tail of the linked list.

Illustration Image

 

Here you can see the linked list originally was 10 -> 20 -> 30 -> 40, but descendingIterator is returning an iterator that is pointing towards 40, i.e., in reverse order.

We need to use â€˜import java.util.Iterator’ in order to use the descendingIterator.

Now let’s look at some coding examples to understand its syntax and working.

Example 1

import java.util.Iterator;
import java.util.LinkedList;

public class DescendingIterator {
   public static void main(String[] args) {
     
       // Creating a new Linked List.
       LinkedList<String> TestList = new LinkedList<>();

      // Adding elements to the list.
       TestList.add("I");
       TestList.add("Love");
       TestList.add("Coding");
       TestList.add("Ninjas");
         
       // descendingIterator pointed to 'IT'.   
       Iterator<String> it = TestList.descendingIterator();
          
       while(it.hasNext()){
            System.out.print(it.next() + " ");
       }
   }
}
You can also try this code with Online Java Compiler
Run Code

Output

Ninjas Coding Love I

Example 2

import java.util.Iterator;
import java.util.LinkedList;

public class DescendingIterator {
   public static void main(String[] args) {
     
       // Creating a new Linked List.
       LinkedList<Integer> TestList = new LinkedList<>();

      // Adding elements to the list.
       TestList.add(1);
       TestList.add(2);
       TestList.add(3);
       TestList.add(4);
         
       // descendingIterator pointed to 'IT'.   
       Iterator<String> it = TestList.descendingIterator();
          
       while(it.hasNext()){
            System.out.print(it.next() + " ");
       }
   }
}
You can also try this code with Online Java Compiler
Run Code

Output

4 3 2 1

Time Complexity

O(N), where ‘N’ is the number of elements in the Linked List.

 As it takes O(N) to reverse the list.

Space Complexity

O(1).

The iterator doesn’t take up any extra space.

Check out this problem - Reverse Nodes In K Group

Frequently Asked Questions

What is a linked list in C++?

A linked list is a linear data structure that instead of storing data at contiguous memory locations, stores data at random locations, and each node is linked to the other by the use of pointers.

What are the applications of a linked list?

1. A linked list is used to implement Stack and Queues.
2. A linked list is used to implement hashing(open chain hashing).
3. A linked list is used to implement graphs(Adjacency list representation of graphs)

How do you delete a node in a linked list?

To delete a node from a linked list, we need to do the following steps.

  1. Find the previous node of the node to be deleted.
  2. Change the next pointer of the previous node.
  3. Free memory for the node to be deleted.

Conclusion

We saw how we could use the inbuilt descendingIterator provided by Java for iterating over the linked list in a reverse fashion. We also saw the example codes and saw their syntax. Your knowledge bank should have grown by now, but you shouldn't stop there because there's still a long way to go. 

Recommended Reading:

Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, Uber, Microsoft, etc. on Coding Ninjas Studio.

Also check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Cheers!

Live masterclass