LinkedList Tutorial In Java With Example

Linked List is a type of Linear Data Structure that is second mostly used data structure after array, which allocates memory dynamically at run time that is it doesn’t require any size initialization as in case of array.

LinkedList stores data in the forms of nodes, which is divided into two parts, first part stores the data and second part points to the next node by storing the address of that node.

In this way it forms a chain of nodes as shown in figure 1.

We have first node which contains Data and Address to next node, and similarly second and third node forming a chain.

link
Important Note: To create code, pattern required is:

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


Table of Contents

Example Of LinkedList:

Program for simple insertion in linked list:

import java.util.Iterator;

import java.util.LinkedList;

public class LinkedListDemo 
{

  public static void main(String[] args) {

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

  linkedList.add("java");

  linkedList.add("Is");

  linkedList.add("High");

  linkedList.add("Level");

  linkedList.add("Language");

  Iterator<String> itr=linkedList.iterator();

  while(itr.hasNext())
  {

   System.out.print(itr.next());

   System.out.print("\t");

  }

 }

}

Output:

java Is High Level Language

Description about program: Here to insert the element in linked list first of all just create the linked list using LinkedList<String> linkedList=new LinkedList<String>(); This will create an empty  linked list of String type for you. Here linkedlist is name of created list. Now for insertion in this, just make use add method already given java.util.LinkedList. simply pass the object in the argument to which you want to add in this. After that an iterator is created which is given in java.util.Iterator. this iterator will move in list from first to last with single move directly.


 Types of Linked List:

Following are the types of Linked List:

1. Singly Linked List:

In Singly linked list node contains data part and address of only forward node. In this type of data structure nodes can be traversed only in forward direction.

2. Doubly Linked List:

In doubly Link List node contains three part, first part contains address of previous node, second part contains the data and the third node contains address of forward node. In this type of data structure nodes can be traversed in both directions that is forward as well as backward.

3. Circular Linked List:

Circular Linked list is similar to singly link list , only difference is last node of the list points to the first node, that is last node contains the address of first node, forming a circular chain.


LinkedList Methods In JAVA:

Let us discuss all the LinkedList methods one by one with Examples in Java.

1. void add(int index, Object element):

This method adds element of Specific Object type at the specified index of the Linked List as  mentioned  the method. If in case the index specified is out of range it throws an IndexOutOfBounds Exception. Example of this method is shown below:

import java.util.*;
public class LinkedListMethods 
{

  public static void main(String[] args) {

  LinkedList llist = new LinkedList();

  llist.add("Hi");

  llist.add("I");

  llist.add("Love");

  llist.add("java");

  System.out.println("LinkedList:" + llist);

//Using Add method at specific index

  llist.add(1,"Element");

  System.out.println("Linked List:" + llist);

 }

}

Output:

LinkedList: [Hi, I, Love, java]

LinkedList: [Hi, Element, I, Love, java]

2. boolean add(Object o):

It adds an element of Specific Object type at the end of Linked List . It gives True if element is successfully added, and gives false if it is not. Example of this method is shown below:

import java.util.*;

public class LinkedListMethods
{

  public static void main(String[] args) {

  LinkedList llist = new LinkedList();

  llist.add("Hi");

  llist.add("I");

  llist.add("Love");

  llist.add("java");

  System.out.println("LinkedList:" + llist);

// using add method

  llist.add("Element");

  System.out.println("Linked List:" + llist);

 }

}

Output:

Linked List: [Hi, I, Love, java, Element]

3. boolean addAll(Collection c):

This method adds each element of the given collection  at the end of the Linked List, It returns True if collection is added successfully, and returns false if it is not. If the collection passed is null then Null Pointer Exception is thrown. Example of this method is shown below:

import java.util.*;

public class LinkedListMethods
{

  public static void main(String[] args) {

  LinkedList llist = new LinkedList();

  llist.add("Hi");

  llist.add("I");

  llist.add("Love");

  llist.add("java");

  System.out.println("Linked List:" + llist);

  Collection collection = new ArrayList();

  collection.add("I");

  collection.add("Love");

  collection.add("Android");

// using method addAll()

  llist.addAll(collection);

  System.out.println("Linked List:" + llist);

 }

}

Output:

Linked List:[Hi, I, Love, java, I, Love, Android]

Read All LinkedList Methods in JAVA with Example


 Basic operations that can be performed on Linked List:

1. Insertion:

To add an Element at the beginning of the linked list.

2. Deletion:

To delete an Element from the beginning of the linked list.

3. Display:

To display all the elements of the linked list.

4. Search:

To search a node from the whole given linked list.

5. Delete:

To delete a particular node from the linked list.


Program to insert element from user defined class in linked list

public class LinkedListHelp 
{

  public String info;

  public int node;

  LinkedListHelp(String info, int node)
  {

    this.info=info;

    this.node=node;

  }

}

 
import java.util.Iterator;

import java.util.LinkedList;

public class LinkedListDemo 
{

  @SuppressWarnings("unchecked")

  public static void main(String[] args) {

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

  LinkedList<LinkedListHelp> linkedList2=new LinkedList<LinkedListHelp>();

  LinkedListHelp object=new LinkedListHelp("node1",01);

  LinkedListHelp object2=new LinkedListHelp("node2",02);

  LinkedListHelp object3=new LinkedListHelp("node3",03);

  linkedList2.add(object);

  linkedList2.add(object2);

  linkedList2.add(object3);

  Iterator itr2=linkedList2.iterator();

  while(itr2.hasNext()){

  LinkedListHelp llD= (LinkedListHelp)itr2.next();

  System.out.print(llD.info);

  System.out.print("\t");

  System.out.print(+llD.node);

  System.out.print("\n");

  }

 }

} 

Output:

node1 1

node2 2

node3 3

Description about the program: In the above program, a class LinkedListHelp is created in which info and node are taken to represent the content of listnode. And in main class its objects are created and after that they were added to linkedlist one by one and printed using an iterator. Method used here is same as in first program.


Program to add content of one list to other using addAll() method

public class LinkedListHelp 
{

  public String info;

  public int node;

  LinkedListHelp(String info, int node)
  {

    this.info=info;

    this.node=node;

  }

}


import java.util.Iterator;

import java.util.LinkedList;

import java.util.Collection;

public class LinkedListDemo 
{

  @SuppressWarnings("unchecked")

  public static void main(String[] args) {

  LinkedList<LinkedListHelp> linkedList=new LinkedList<LinkedListHelp>();

  LinkedList<LinkedListHelp> linkedList2=new LinkedList<LinkedListHelp>();

  LinkedListHelp object=new LinkedListHelp("node1",01);

  LinkedListHelp object2=new LinkedListHelp("node2",02);

  LinkedListHelp object3=new LinkedListHelp("node3",03);

  LinkedListHelp object5=new LinkedListHelp("node5",01);

  LinkedListHelp object6=new LinkedListHelp("node6",02);

  LinkedListHelp object7=new LinkedListHelp("node7",03);

  linkedList.add(object);

  linkedList.add(object2);

  linkedList.add(object3);

  linkedList2.add(object5);

  linkedList2.add(object6);

  linkedList2.add(object7);

  linkedList.addAll(linkedList2);

  Iterator itr=linkedList.iterator();

  while(itr.hasNext()){

  LinkedListHelp llD= (LinkedListHelp)itr.next();

  System.out.print(llD.info);

  System.out.print("\t");

  System.out.print(+llD.node);

  System.out.print("\n");

  }

 }

} 

Output:

node1 1

node2 2

node3 3

node5 1

node6 2

node7 3

Description about the program: Here in the above program two different list were created using concept of class and different objects were added into both the lists. And then by using special method addAll content of second list were appended into first list. The syntax used for appending in the list is linkedList.addAll(linkedList2);where linkedlist is variable name representing the first list, addAll is method used for appending the elements and linkedlist2 is name for second list which is appended.


Program to insert inbetween, FirstPostion and LastPosition using Add(indexOf, object), addFirst, addLast methods in the linked list

public class LinkedListHelp 
{
  public String info;

  public int node;

  LinkedListHelp(String info, int node)
  {

    this.info=info;

    this.node=node;

  }

}

 
import java.util.Iterator;

import java.util.LinkedList;

import java.util.Collection;

public class LinkedListDemo 
{

  @SuppressWarnings("unchecked")

  public static void main(String[] args) {

  LinkedList<LinkedListHelp> linkedList=new LinkedList<LinkedListHelp>();

  LinkedList<LinkedListHelp> linkedList2=new LinkedList<LinkedListHelp>();

  LinkedListHelp object=new LinkedListHelp("node1",01);

  LinkedListHelp object2=new LinkedListHelp("node2",02);

  LinkedListHelp object3=new LinkedListHelp("node3",03);

  LinkedListHelp object5=new LinkedListHelp("node5",01);

  LinkedListHelp object6=new LinkedListHelp("node6",02);

  LinkedListHelp object7=new LinkedListHelp("node7",03);

  linkedList.add(object);

  linkedList.add(object2);

  linkedList.add(object3);
  linkedList2.add(object5);

  linkedList2.add(object6);

  linkedList2.add(object7);

  System.out.println("After the addtion of all elements from second list to first list is now");

  linkedList.addAll(linkedList2);

  Iterator itr=linkedList.iterator();

  while(itr.hasNext()){

  LinkedListHelp llD= (LinkedListHelp)itr.next();

  System.out.print(llD.info);

  System.out.print("\t");

  System.out.print(+llD.node);

  System.out.print("\t");

  }

  LinkedListHelp object4=new LinkedListHelp("node4",04);

  linkedList.add(3, object4);

  System.out.println();

  System.out.println("after addition of object4 at index 3");

  Iterator itr1=linkedList.iterator();

  while(itr1.hasNext()){

  LinkedListHelp llD= (LinkedListHelp)itr1.next();

  System.out.print(llD.info);

  System.out.print("\t");

  System.out.print(+llD.node);

  System.out.print("\t");

  }

  LinkedListHelp object0=new LinkedListHelp("node0",00);

  linkedList.addFirst(object0);

  System.out.println();

  System.out.println("After the addtion of obect0 in list at first postion or you can say at 0 index");

  Iterator itr2=linkedList.iterator();

  while(itr2.hasNext()){

  LinkedListHelp llD= (LinkedListHelp)itr2.next();

  System.out.print(llD.info);

  System.out.print("\t");

  System.out.print(+llD.node);

  System.out.print("\t");

  }

  LinkedListHelp object8=new LinkedListHelp("node8",4);

  linkedList.addLast(object8);

  System.out.println();

  System.out.println("After addtion of obect at last postion the list will be");

  Iterator itr3=linkedList.iterator();

  while(itr3.hasNext()){

  LinkedListHelp llD= (LinkedListHelp)itr3.next();

  System.out.print(llD.info);

  System.out.print("\t");

  System.out.print(+llD.node);

  System.out.print("\t");

  }

 } 

}

Output:

After the addtion of all elements from second list to first list is now

node1 1     node2 2     node3 3     node5 1     node6 2     node7 3

after addition of object4 at index 3

node1 1     node2 2     node3 3     node4 4     node5 1     node6 2     node7      3

After the addtion of obect0 in list at first postion or you can say at 0 index

node0 0     node1 1     node2 2     node3 3     node4 4     node5 1     node6      2     node7 3

After addtion of obect at last postion the list will be

node0 0     node1 1     node2 2     node3 3     node4 4     node5 1     node6      2     node7 3     node8 4

Description about the program: As from the output its much clear what is happening in this program. But for better understanding it is important to know how its occurring. Like if want to insert an element in between the list there is method used add(index, elementtoadd). This method can be used used to add object at any particular index of given list. If you have to insert the element at firstpsoition which is termed as firstnode of list simple addFirst() method is called. Using this element will be added at firstposition in the list. And if you have to add the element at last position method used is addLast(). This method simply insert the element at last position in the given list.


Advantages of Linked List:

  • Linked list doesn’t require size initialization in advance, which makes it dynamic in nature.
  • Inserting as well as deleting an element can be very easily performed
  • Modification operations can be performed in very less time, as it has to just change the address in the given nodes.

Disadvantages of Linked List:

  • Lot of memory is wasted to store the address of each next node with each data.
  • We cannot access any node in random fashion, it can only be traversed in sequential way.
  • In link list we cannot traverse it in backward direction.

Important Points To Remember:

  • Using Linked List feature of pointing to the next element, that is storing the address of next element it let you implement  graphs, stacks, queues  and many more data structures using this feature.
  • Using linked lists we can easily insert and delete elements at any position very easily by just changing the address part of the node .
  • Linked list doesn’t require any prior initialization of size as compared to array which requires declaration of memory before using it.

Leave a Reply

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