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).
Syntax:
public Iterator descendingIterator()
Return Value: This method returns an iterator over the elements in this LinkedList in reverse sequence.
Below are the examples to illustrate the descendingIterator() method
Example 1:
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
try {
LinkedList<String> list = new LinkedList<String>();
list.add( "A" );
list.add( "B" );
list.add( "C" );
System.out.println( "LinkedList:" + list);
Iterator x = list.descendingIterator();
while (x.hasNext()) {
System.out.println( "Value is : "
+ x.next());
}
}
catch (NullPointerException e) {
System.out.println( "Exception thrown : "
+ e);
}
}
}
|
Output:
LinkedList:[A, B, C]
Value is : C
Value is : B
Value is : A
Example 2:
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
try {
LinkedList<Integer>
list = new LinkedList<Integer>();
list.add( 10 );
list.add( 20 );
list.add( 30 );
System.out.println( "LinkedList:" + list);
Iterator x = list.descendingIterator();
while (x.hasNext()) {
System.out.println( "Value is : "
+ x.next());
}
}
catch (NullPointerException e) {
System.out.println( "Exception thrown : " + e);
}
}
}
|
Output:
LinkedList:[10, 20, 30]
Value is : 30
Value is : 20
Value is : 10
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!