LinkedList listIterator()
The Java.util.LinkedList.listIterator() method returns a list-iterator containing the same elements as that of the LinkedList and in the same sequence starting from a specific position or index number. The position or index is passed as a parameter to the function.
Syntax
ListIterator new_list = LinkedList.listIterator(int index);
Parameter: A parameter is an integer number that indicates the element from which ListIterator begins functioning and returning values.
Return value: The method returns a sub-list of the original starting from the specified index.
Example
import java.io.*;
import java.util.LinkedList;
import java.util.ListIterator;
public class LinkedListTest {
public static void main(String args[])
{
//creating a demo list
LinkedList<String> demoList = new LinkedList<String>();
// Using add() method to add elements in the demoList
demoList .add("Coding");
demoList .add("Ninjas");
demoList .add("Studio");
demoList .add("10");
demoList .add("20");
// Displaying the linkedlist
System.out.println("LinkedList items:" + list);
// Setting the ListIterator at a specified index
ListIterator listIter = demoList .listIterator(2);
// Iterating through the new list from the position
System.out.println("The list using listIterator is as follows:");
while(listIter.hasNext()){
System.out.println(listIter.next());
}
}
}

You can also try this code with Online Java Compiler
Run Code
Output
LinkedList items:[Coding, Ninjas, Studio, 10, 20]
The list is as follows:
Studio
10
20

You can also try this code with Online Java Compiler
Run Code
Frequently Asked Questions
listIterator is in which java package?
listIterator is in java.util package
What does the parameter passed to listIterator function indicate?
The parameter passed to listIterator indicates the starting position from where the iterator starts operating.
What is the syntax for the listIterator function?
ListIterator new_list = LinkedList.listIterator(int index);
Conclusion
- The Java.util.LinkedList.listIterator() method returns a list-iterator containing the same elements as that of the LinkedList and in the same sequence starting from a specific position or index number. The position or index is passed as a parameter to the function.
- An index parameter is an integer number that indicates the element from which ListIterator begins functioning and returning values..
- The method returns a sub-list of the original starting from the specified index.
Recommended Reading:
Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, Uber, Microsoft, etc. on Coding Ninjas Studio.
Also check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.
Happy learning!!