Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

# Linked List descendingIterator in Java with Examples

Last Updated on December 13, 2022 by Prepbytes

This blog contains an important linked list topic â€śdescendingiterator in javaâ€ť. descendingiterator in java will make your command stronger on Java and data structures like linked list. Letâ€™s discuss what is descendingiterator in java for linked list.

The descendingIterator() method returns an iterator over the elements of the LinkedList in reverse order. That means the elements will be returned in order from the last element to the first element, i.e. tail to head.

We know that in Java, LinkedList is implemented as Doubly LinkedList, so we can start from the tail pointer and iterate the list reversely. For the purpose of traversal, we use an iterator and, descendingiterator in java helps us in traversing the list from tail pointer to head pointer.

Syntax

Returns:descendingiterator in java returns an iterator over the elements in reverse sequence.

The necessary imports that you have to do are:

• import java.util.Iterator;

Following are the steps we have used to demonstrate the working of the descendingIterator() method.

• After that, we have used LinkedList descendingIterator() method to get the iterator in reverse order.
• Finally, Traversed the LinkedList and printed elements.

### Example 1

```import java.util.Iterator;

public class Main {
public static void main(String[] args) {

Iterator<string> iterator = list.descendingIterator();
System.out.println("The cities are: ");
while(iterator.hasNext()){
System.out.print(iterator.next() + " ");
}
}
}
```
``````Output
The cities are:

Time Complexity For descendingiterator In Java For Linked List: O(n), where n is the no of entries in the list. Here in our example, it is 5.

Output Explanation:
Even though the structure of Linked List is:
We are getting results in reverse sequence with the help of the descendingIterator() method of LinkedList.

### Example 2

```import java.util.Iterator;

public class Main {
public static void main(String[] args) {

Iterator<integer> iterator = list.descendingIterator();
System.out.println("The Integers in Reversed Order are: ");
while(iterator.hasNext()){
System.out.print(iterator.next() + " ");
}
}
}
```
``````Output:
The Integers in Reversed Order are:
5 4 3 2 1``````

Time Complexity For descendingiterator In Java For Linked List: O(n), where n is the no of entries in the list. Here in our example, it is 5.

Output Explanation: The Structure of our linked list is: 1 â†’ 2 â†’ 3 â†’ 4 â†’ 5.
With the help of the descendingIterator() method of the linked list, we can traverse the list in reverse order.

This blog explains an amazing topic â€śTo descendingiterator In Java For Linked Listâ€ť. Practicing topics like descendingiterator in java will make your excellent command on Java and on linked list, having knowledge about data structures like linked list always have the upper hand for conquering your dream, you can follow this link Linked List to practice more.

## FAQ

1. What is descendingIterator in Java?
The descendingIterator() method of java. util. LinkedList class is used to return an iterator over the elements in this LinkedList in reverse sequential order. The elements will be returned in order from last (tail) to first (head).

2. What is the linked list in Java?
Linked List is a part of the Collection framework present in java. util package. This class is an implementation of the LinkedList data structure which is a linear data structure where the elements are not stored in contiguous locations and every element is a separate object with a data part and address part.

3. How is the linked list used?

• Applications of linked lists in computer science: Implementation of stacks and queues.
• Implementation of graphs: Adjacency list representation of graphs is the most popular which uses a linked list to store adjacent vertices.
• Dynamic memory allocation: We use a linked list of free blocks.