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

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

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;

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<string> list = new LinkedList<>();
        list.add("Mumbai");
        list.add("Delhi");
        list.add("Kolkata");
        list.add("Bangalore");
        list.add("Hyderabad");

        Iterator<string> 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 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:
"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<integer> list = new LinkedList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);

        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.

Leave a Reply

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