LinkedList with Looping

In this article, we will see how to loop LinkedList in java

It’s very much common requirement to iterate or loop through LinkedList in java applications

There are mainly 4 ways to loop through LinkedList in java


1) Traditional For loop
2) Enhanced For loop
3) While loop
4) Iterator

Let’s see each of these ways with an example

1) Traditional For loop
  1. import java.util.*;
  2. public class LinkedListTraditionalForLoop {
  3.     public static void main(String[] args) {
  4.     LinkedList<Integer> list = new LinkedList<Integer>();
  5.       list.add(14);
  6.       list.add(7);
  7.       list.add(21);
  8.       list.add(28);
  9.      System.out.println("Traditional For Loop");
  10.       for (int index = 0; index < list.size(); index++) {            
  11.           System.out.println(list.get(index));     
  12.       }  
  13.     }
  14. }
import java.util.*;
public class LinkedListTraditionalForLoop {
	public static void main(String[] args) {
	LinkedList<Integer> list = new LinkedList<Integer>();
      list.add(14);
      list.add(7);
      list.add(21);
      list.add(28);
	 System.out.println("Traditional For Loop");
      for (int index = 0; index < list.size(); index++) { 		      
          System.out.println(list.get(index)); 		
      }  
	}
}
Note : In the above example, we have used Traditional for loop to iterate the LinkedList

2) Enhanced For loop
  1. import java.util.*;
  2. public class LinkedListEnhancedForLoop {
  3.     public static void main(String[] args) {
  4.     LinkedList<Integer> list = new LinkedList<Integer>();
  5.       list.add(14);
  6.       list.add(7);
  7.       list.add(21);
  8.       list.add(28);
  9.      System.out.println("Enhanced For Loop");
  10.       for (Integer element : list) {             
  11.            System.out.println(element);        
  12.       }
  13.  
  14.     }
  15. }
import java.util.*;
public class LinkedListEnhancedForLoop {
	public static void main(String[] args) {
	LinkedList<Integer> list = new LinkedList<Integer>();
      list.add(14);
      list.add(7);
      list.add(21);
      list.add(28);
	 System.out.println("Enhanced For Loop");
      for (Integer element : list) { 		      
           System.out.println(element); 		
      }

	}
}
Note : In the above example, we have used advanced/enhanced for loop to iterate LinkedList

3) While loop
  1. import java.util.*;
  2. public class LinkedListWhileLoop {
  3.     public static void main(String[] args) {
  4.     LinkedList<Integer> list = new LinkedList<Integer>();
  5.       list.add(14);
  6.       list.add(7);
  7.       list.add(21);
  8.       list.add(28);
  9.       System.out.println("While Loop");        
  10.       int count = 0;       
  11.       while ( count < list.size()) {
  12.      System.out.println(list.get(count));
  13.          count++;
  14.       }
  15.  
  16.  
  17.     }
  18. }
import java.util.*;
public class LinkedListWhileLoop {
	public static void main(String[] args) {
	LinkedList<Integer> list = new LinkedList<Integer>();
      list.add(14);
      list.add(7);
      list.add(21);
      list.add(28);
	  System.out.println("While Loop"); 		
      int count = 0; 		
      while ( count < list.size()) {
	 System.out.println(list.get(count));
         count++;
      }


	}
}
Note : In the above example, we are using while loop to iterate linkedlist

4) Iterator
  1. import java.util.*;
  2. public class LinkedListWhileIterator {
  3.     public static void main(String[] args) {
  4.     LinkedList<Integer> list = new LinkedList<Integer>();
  5.       list.add(14);
  6.       list.add(7);
  7.       list.add(21);
  8.       list.add(28);
  9.       System.out.println("While with iterator");
  10.       Iterator itr = list.iterator();
  11.       while (itr.hasNext()) {
  12.          System.out.println(itr.next());
  13.       }
  14.  
  15.  
  16.  
  17.     }
  18. }
import java.util.*;
public class LinkedListWhileIterator {
	public static void main(String[] args) {
	LinkedList<Integer> list = new LinkedList<Integer>();
      list.add(14);
      list.add(7);
      list.add(21);
      list.add(28);
	  System.out.println("While with iterator");
      Iterator itr = list.iterator();
      while (itr.hasNext()) {
         System.out.println(itr.next());
      }



	}
}
Note : In the above example, we have used iterator with while loop to iterate linkedlist

About the Author

Karibasappa G C (KB)
Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on