LinkedList descendingIterator in Java with Examples

Introduction

One of the most crucial data structures to learn while preparing for interviews is the linked list. In a coding interview, having a thorough understanding of Linked Lists might be a major benefit.

Introduction

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() helps us in traversing the list from tail pointer to head pointer.

Syntax

  • Iterator java.util.LinkedList.descendingIterator();

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

The necessary imports that you have to do are:

  • import java.util.Iterator;

LinkedList descendingIterator() method Examples

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

  • Created a LinkedList instance using new LinkedList().
  • Then, we have added some elements to LinkedList.
  • 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;
import java.util.LinkedList;

public class Main {
    public static void main(String[] args) {
        LinkedList list = new LinkedList<>();
        list.add("Mumbai");
        list.add("Delhi");
        list.add("Kolkata");
        list.add("Bangalore");
        list.add("Hyderabad");

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

The cities are:
Hyderabad Bangalore Kolkata Delhi Mumbai

Time Complexity: 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:
"Mumbai"->"Delhi"->"Kolkata"->"Bangalore"->"Hyderabad"
We are getting results in reverse sequence with the help of the descendingIterator() method of LinkedList.

Example 2

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

public class Main {
    public static void main(String[] args) {
        LinkedList list = new LinkedList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);

        Iterator 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: 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 tried to discuss the descendingIterator method of the LinkedList. If you want to practice more questions on linked lists, feel free to solve them on Linked List. If you want to solve more questions on Linked List, which are curated by our expert mentors at PrepBytes, you can follow this link Linked List.

Previous post Recursive selection sort for singly linked list | Swapping node links
Next post Java.util.LinkedList.offer(), offerFirst(), offerLast() in Java

Leave a Reply

Your email address will not be published. Required fields are marked *