📜  在Java中以相反的顺序迭代 LinkedList

📅  最后修改于: 2022-05-13 01:55:16.281000             🧑  作者: Mango

在Java中以相反的顺序迭代 LinkedList

为了以相反的顺序遍历链表,我们可以使用降序迭代器列表迭代器

1. 降序迭代器

句法:

LinkedList linkedlist = new LinkedList<>();

Iterator listIterator = linkedlist.descendingIterator();

返回: Descending Iterator 返回指向链表末尾的 Iterator。

2.列表迭代器

句法:

LinkedList linkedlist = new LinkedList<>();

ListIterator listIerator = linkedlist.listIterator(linkedlist.size());

参数:链表的大小,这将使迭代器指向链表的末尾。

示例 1:使用降序迭代器

Java
// Java program to Iterate a LinkedList in Reverse Order
// using descending Iterator
  
import java.util.Iterator;
import java.util.LinkedList;
  
public class GFG {
    public static void main(String[] args)
    {
        LinkedList linkedList = new LinkedList<>();
        
        // adding elements to linked list
        linkedList.add("Geeks");
        linkedList.add("For");
        linkedList.add("Geek");
        linkedList.add("2020");
        linkedList.add("2021");
  
        // getting an iterator which points at the
        // end of the linkedlist
        Iterator iterator = linkedList.descendingIterator();
  
        // traversing the linkedlist
        // hasNext() will tell if previous element is
        // available or not
        // next() with descending iterator will return the
        // previous element
        // and after getting the previous element
        // is moves the cursor to next previous element.
        while (iterator.hasNext()) 
        {
            System.out.println(iterator.next());
        }
    }
}


Java
// Java program to Iterate a LinkedList in Reverse Order
// using List Iterator
  
import java.util.LinkedList;
import java.util.ListIterator;
  
public class GFG {
    public static void main(String[] args)
    {
  
        LinkedList linkedList = new LinkedList<>();
  
        // adding elements of to the linkedlist
        linkedList.add("Geeks");
        linkedList.add("For");
        linkedList.add("Geek");
        linkedList.add("2020");
        linkedList.add("2021");
  
        // getting an iterator that points at the end of the
        // linkedlist
        ListIterator listIterator = linkedList.listIterator(linkedList.size());
  
        // Traversing the linked list
        // hasPrevious() function to check if previous
        // element is present or not previous() function to
        // get the previous element and after getting
        // previous elements it move the cursor to the next
        // previous element
        while (listIterator.hasPrevious())
        {
            System.out.println(listIterator.previous());
        }
    }
}


输出
2021
2020
Geek
For
Geeks

示例 2:使用列表迭代器

Java

// Java program to Iterate a LinkedList in Reverse Order
// using List Iterator
  
import java.util.LinkedList;
import java.util.ListIterator;
  
public class GFG {
    public static void main(String[] args)
    {
  
        LinkedList linkedList = new LinkedList<>();
  
        // adding elements of to the linkedlist
        linkedList.add("Geeks");
        linkedList.add("For");
        linkedList.add("Geek");
        linkedList.add("2020");
        linkedList.add("2021");
  
        // getting an iterator that points at the end of the
        // linkedlist
        ListIterator listIterator = linkedList.listIterator(linkedList.size());
  
        // Traversing the linked list
        // hasPrevious() function to check if previous
        // element is present or not previous() function to
        // get the previous element and after getting
        // previous elements it move the cursor to the next
        // previous element
        while (listIterator.hasPrevious())
        {
            System.out.println(listIterator.previous());
        }
    }
}
输出
2021
2020
Geek
For
Geeks