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!

How To Iterate LinkedList in Java

Last Updated on December 13, 2022 by Prepbytes

Introduction

One of the most crucial data structures to learn while preparing for interviews is the linked list. In a coding interview, having a thorough understanding of LinkedList might be a significant benefit.

In this article, we will see how to iterate a linked list in java.

Ways to iterate LinkedList in java
1) Using loop:

  • for loop
  • while loop
  • do while loop
    2) Using enhanced for loop.
    3) Using forEach() method.
    4) Using iterators
  • iterator() method
  • listIterator() method
    5) Using isEmpty() and poll() method
    6) Using streams.

Approach 1

Using for loop

The below program shows how to iterate a linked list in java using for loop.

Code Implementation:

import java.util.LinkedList;
public class Prepbytes {

    public static void main(String[] args) {
        /*
         * Create an empty LinkedList
         */
        LinkedList<Integer> list = new LinkedList<Integer>();

        // Add values to the list
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(9);
        list.add(11);
        
        // Iterate using for loop
        System.out.print("Iterating the linked list using for loop : ");
        for(int i = 0 ;i<list.size();i++) {
            System.out.print(list.get(i) + " ");
        }
    }    
}



Output

Iterating the linked list using for loop : 1 2 3 4 9 11

Time Complexity: O(n), as we are traversing the list, and n is the number of nodes in the linked list.
Space Complexity: O(1), Constant auxiliary space is being used.

Approach 2

Using while loop

The below program shows how to iterate a linked list in java using while loop.

Code Implementation

import java.util.LinkedList;
public class Prepbytes {

    public static void main(String[] args) {
        /*
         * Create an empty LinkedList
         */
        LinkedList<integer> list = new LinkedList<integer>();

        // Add values to the list
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(9);
        list.add(11);
        
        // Iterate using while loop
     System.out.print("Iterating the linked list using while loop : ");
        int i = 0;
        while(i < list.size()) {
            System.out.print(list.get(i) + " ");
            i++;
        }
   }    
}

Output

Iterating the linked list using while loop : 1 2 3 4 9 11

Time Complexity: O(n), as we are traversing the list, and n is the number of nodes in the linked list.
Space Complexity: O(1), Constant auxiliary space is being used.

Approach 3

Using do while loop

The below program shows how to iterate a linked list in java using do while loop.

Code Implementation

import java.util.LinkedList;
public class Prepbytes {

    public static void main(String[] args) {
        /*
         * Create an empty LinkedList
         */
        LinkedList list = new LinkedList();

        // Add values to the list
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(9);
        list.add(11);
        
        // Iterate using do while loop
     System.out.print("Iterating the linked list using do while loop : ");
        int i = 0;
        do {
            System.out.print(list.get(i) + " ");
            i++;
        }
     while(i < list.size());

    }
}

Output

Iterating the linked list using do while loop : 1 2 3 4 9 11

Time Complexity: O(n), as we are traversing the list, and n is the number of nodes in the linked list.
Space Complexity: O(1), Constant auxiliary space is being used.

Approach 4

Using enhanced for loop

The below program shows how to iterate a linked list in java using enhanced for loop.

Code Implementation

import java.util.LinkedList;
public class Prepbytes {
    public static void main(String[] args) {
        /*
         * Create an empty LinkedList
         */
        LinkedList<integer> list = new LinkedList<integer>();

        // Add values to the list
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(9);
        list.add(11);
        
        // Iterate using Enhanced for loop
     System.out.print("Iterating the linked list using enhanced for loop : ");

        for(Integer element : list) {
            System.out.print(element + " ");
        }
   }    
}

Output

Iterating the linked list using enhanced for loop : 1 2 3 4 9 11

Time Complexity: O(n), as we are traversing the list, and n is the number of nodes in the linked list.
Space Complexity: O(1), Constant auxiliary space is being used.

Approach 5

Using forEach() method

The below program shows how to iterate a linked list in java using forEach() method.

Syntax:

forEach(Consumer action)

Return value

void

Code Implementation

import java.util.LinkedList;
public class Prepbytes {

    public static void main(String[] args) {
        /*
        * Create an empty LinkedList
        */
        LinkedList<integer> list = new LinkedList<integer>();

        // Add values to the list
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(9);
        list.add(11);
     
        // Iterate using forEach loop
        System.out.print("Iterating the linked list using forEach() method : ");
        list.forEach((element) -> System.out.print(element + " "));
    }    
}

Output

Iterating the linked list using forEach() method : 1 2 3 4 9 11

Time Complexity: O(n), as we are traversing the list and n is the number of nodes in the linked list.
Space Complexity: O(1), Constant auxiliary space is being used because forEach internally iterates over the elements of collections.

Approach 6

Using Iterator() method

The below program shows how to iterate a linked list in java using iterator() method.

Syntax

LinkedList.iterator()

Return value

The LinkedList.iterator() method returns an iterator over the sequence of elements of the Linked List in proper order.

Code Implementation

import java.util.LinkedList;
import java.util.Iterator;
public class Prepbytes {

    public static void main(String[] args) {
        /*
         * Create an empty LinkedList
         */
        LinkedList<integer> list = new LinkedList<integer>();

        // Add values to the list
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(9);
        list.add(11);
        
        // Iterate using iterator
      System.out.print("Iterating the linked list using iterator : ");

        Iterator<integer> itr = list.iterator();
        while(itr.hasNext()) {
            System.out.print(itr.next()+ " ");
        }

    }    
}

Output

Iterating the linked list using iterator : 1 2 3 4 9 11

Time Complexity: O(n), where n is the number of elements in the linked list.
Space Complexity: O(1), Constant auxiliary space is being used because iterator() creates an iterator over the elements of the list.

Approach 7

Using listIterator() method

The below program shows how to iterate a linked list in java using listIterator() method.

Syntax

LinkedList.listIterator()

Return value

The LinkedList.listIterator() method returns a list iterator over the sequence of elements of the Linked List in proper order.

Code Implementation

import java.util.LinkedList;
import java.util.Iterator;
public class Prepbytes {

    public static void main(String[] args) {
        /*
         * Create an empty LinkedList
         */
        LinkedList list = new LinkedList();

        // Add values to the list
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(9);
        list.add(11);
        
        // Iterate using iterator
     System.out.print("Iterating the linked list using listIterator() method : ");

        Iterator itr = list.listIterator();
        while(itr.hasNext()) {
            System.out.print(itr.next()+ " ");
        }

    }    
}

Output

Iterating the linked list using listIterator() method : 1 2 3 4 9 11

Time Complexity: O(n), where n is the number of elements in the linked list.
Space Complexity: O(1), Constant auxiliary space is being used because listIterator() creates a list iterator over the elements of the list.

Approach 8

Using isEmpty() and poll() methods

The below program shows how to iterate a linked list in java using isEmpty() and poll() method.

Syntax

  • LinkedList.isEmpty()

  • LinkedList.poll()

Return value

  • isEmpty() method returns true if this list is empty, false otherwise.

  • poll() method retrieves and removes the head of this list.

Code Implementation

import java.util.LinkedList;
public class Prepbytes {

    public static void main(String[] args) {
        /*
         * Create an empty LinkedList
         */
        LinkedList<integer> list = new LinkedList<integer>();

        // Add values to the list
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(9);
        list.add(11);

     
       System.out.print("Iterating the linked list using isEmpty() and poll() method : ");
 
        while(!list.isEmpty()) {
            System.out.print(list.poll()+ " ");
        }
    }    
}

Output

Iterating the linked list using isEmpty() and poll() method : 1 2 3 4 9 11

Time Complexity: O(n), where n is the number of elements in the linked list.
Space Complexity: O(1), Constant auxiliary space is being used.

Approach 9

Using streams

The below program shows how to iterate a linked list in java using streams.

Code Implementation

import java.util.LinkedList;
import java.util.stream.Stream;
public class Prepbytes {

    public static void main(String[] args) {
        /*
         * Create an empty LinkedList
         */
        LinkedList<integer> list = new LinkedList<integer>();

        // Add values to the list
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(9);
        list.add(11);
        
        Stream<integer> stream = list.stream();
 
       System.out.print("Iterating the linked list using streams : "); 
        stream.forEach(s -> System.out.print(s+" "));
    }    
}

Output

Iterating the linked list using streams : 1 2 3 4 9 11

Time Complexity: O(n), where n is the number of elements in the linked list.
[forminator_quiz id=”5382″]

So, in this blog, we have learned different ways to iterate a LinkedList in java. If you want to practice questions on linked list, feel free to solve them at Prepbytes (Linked List).

Leave a Reply

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