Java Program to Iterate Linked List Elements From the Specified Position

You are currently viewing Java Program to Iterate Linked List Elements From the Specified Position

Java program to iterate linked list elements from the specified position

// Java program to iterate linked list elements 
// from specified position

import java.util.LinkedList;
import java.util.ListIterator;

public class Main {
  public static void main(String[] args) {
    LinkedList < String > countries = new LinkedList < String > ();

    countries.add("India");
    countries.add("USA");
    countries.add("UK");
    countries.add("CANADA");
    countries.add("USA");
    countries.add("PAK");

    ListIterator itr = countries.listIterator(2);

    System.out.println("Countries are:");
    while (itr.hasNext()) {
      System.out.print(itr.next() + " ");
    }
  }
}

Output

Countries are:
UK CANADA USA PAK 

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply