LinkedList overview

LinkedList is a doubly-linked list implementation of the List and Deque interfaces
Hence it provides linked list data structure

Important features of LinkedList


1) LinkedList maintains the insertion order similar to ArrayList
2) LinkedList contains duplicate elements
3) It’s a non-synchronized class
4) Elements can be searched in forward and backward directions
5) Searching an element takes time if the size of list is big as it has to traverse the list
6) Since it’s a doubly linked list, elements can be added/removed from both the sides

Doubly_linked_list

LinkedList object can be created as below

  1. LinkedList<String> list = new LinkedList<String>();
LinkedList<String> list = new LinkedList<String>();


Example of Linked list with some of its methods

  1. import java.util.*;
  2. public class LinkedListExample {
  3.      public static void main(String args[]) {
  4.  
  5.        LinkedList<String> linkedlist = new LinkedList<String>();
  6.  
  7.        linkedlist.add("Welcome");
  8.        linkedlist.add("to");
  9.        linkedlist.add("java");
  10.  
  11.        System.out.println("Linked List -> " +linkedlist);
  12.  
  13.        
  14.        linkedlist.addFirst("Hai");
  15.        linkedlist.addLast("Bye");
  16.        System.out.println("LinkedList after addition -> " +linkedlist);
  17.  
  18.        Object firstEle1 = linkedlist.get(0);
  19.        System.out.println("First element: " +firstEle1);
  20.        linkedlist.set(0, "Updated Hai");
  21.        Object firstEle2 = linkedlist.get(0);
  22.        System.out.println("First element after updating -> " +firstEle2);
  23.  
  24.        linkedlist.removeFirst();
  25.        linkedlist.removeLast();
  26.        System.out.println("LinkedList after deletion of first and last element: " +linkedlist);
  27.  
  28.        linkedlist.add(0, "New Element");
  29.        linkedlist.remove(2);
  30.        System.out.println("Final linkedlist-> " +linkedlist);
  31.      }
  32. }
import java.util.*;
public class LinkedListExample {
     public static void main(String args[]) {

       LinkedList<String> linkedlist = new LinkedList<String>();

       linkedlist.add("Welcome");
       linkedlist.add("to");
       linkedlist.add("java");
  
       System.out.println("Linked List -> " +linkedlist);

       
       linkedlist.addFirst("Hai");
       linkedlist.addLast("Bye");
       System.out.println("LinkedList after addition -> " +linkedlist);

       Object firstEle1 = linkedlist.get(0);
       System.out.println("First element: " +firstEle1);
       linkedlist.set(0, "Updated Hai");
       Object firstEle2 = linkedlist.get(0);
       System.out.println("First element after updating -> " +firstEle2);

       linkedlist.removeFirst();
       linkedlist.removeLast();
       System.out.println("LinkedList after deletion of first and last element: " +linkedlist);

       linkedlist.add(0, "New Element");
       linkedlist.remove(2);
       System.out.println("Final linkedlist-> " +linkedlist); 
     }
}

About the Author

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