Java – LinkedList class with example

In this article, we will discuss LinkedList class – one of the List implemented class in detail

1. LinkedList:

  • LinkedList is implementation class of List interface (i.e.; LinkedList implements List)
  • LinkedList uses doubly-linked list to store element/objects
  • Duplicate element/objects are allowed to be inserted into LinkedList
  • Insertion order is maintained
  • Allows NULL insertion (no limit, we can insert any number of NULL to LinkedList)
  • Without generics, LinkedList allows to insert any type of objects;
  • With generics, it is type-bounded (except, if we take Object as type within angle brackets)
  • Manipulation (i.e.; addition/deletion/updation of element from middle of LinkedList) is very fast, as it works on the basis of previous & next nodes and there is no shifting required
  • Elements retrieval is much slower, as LinkedList need to iterate over complete list to retrieve/get the required item/element
  • LinkedList is non-synchronized
  • Present in java.util package and extends java.util.AbstractSequentialList implements java.util.List interface
  • Also, implements java.lang.Cloneable, marker interfaces which provides special ability to LinkedList (provided by JVM at run time) like,
  • java.lang.Cloneable: to create a duplicate object or to clone an object
  • to transfer objects across network

Source: Team BenchResources.Net

2. LinkedList constructors:

2.1 LinkedList ll = new LinkedList();

  • creates an empty LinkedList object

2.2 LinkedList ll = new LinkedList(Collection c);

  • creates a LinkedList for the specified collection
  • it is basically used for inter-conversion between collection objects

3. LinkedList methods:

  • We can use LinkedList as Stack or Queue.
  • To work with stack or queue,
  • LinkedList defines more specific methods as listed below
LinkedList methodsDescription
void addFirst(Object obj);add/inserts the specified element/object at the beginning of the invoking list
void addLast(Object obj);add/inserts the specified element/object at the end of the invoking list
Object getFirst();returns first element/object from invoking list


throws NoSuchElelementException; if list is empty

Object getLast();returns last element/object from invoking list


throws NoSuchElelementException; if list is empty

Object removeFirst();removes & returns first element/object from invoking list


throws NoSuchElelementException; if list is empty

Object removeLast();removes & returns last element/object from invoking list


throws NoSuchElelementException; if list is empty

Iterator descendingIterator();Returns an iterator over the elements in this deque in reverse sequential order


The elements will be returned in order from last (tail) to first (head)

Available from Java 1.6 version

4. LinkedList examples:


import java.util.LinkedList;

public class LinkedListAddAndRemove {

	public static void main(String[] args) {

		// creating LinkedList object of type String
		LinkedList<String> ll = new LinkedList<String>();

		// adding elements to LinkedList object
		ll.add("Sundar Pichai");
		ll.add("Satya Nadella");
		ll.add("Shiv Nadar");
		ll.add("Shantanu Narayen");
		ll.add("Sundar Pichai");
		ll.add("Francisco D’Souza");

		System.out.println("Iterating LinkedList values\n");

		// Iterating using enhanced for-loop
		for(String str : ll){

		// removing element at 4th index

		// to print all values of LinkedList
		System.out.println("\n\nLinkedList values after removal"
				+ " at 4th index postion \n" + ll);


Iterating LinkedList values

Sundar Pichai
Satya Nadella
Shiv Nadar
Shantanu Narayen
Sundar Pichai
Francisco D’Souza

LinkedList values after removal at 4th index postion
[Sundar Pichai, Satya Nadella, Shiv Nadar,
Shantanu Narayen, Francisco D’Souza]

Note: All methods of LinkedList is non-synchronized

Q) How to make LinkedList synchronized ?

  • LinkedList can be easily converted into synchronized LinkedList using utility method of java.util.Collections class
List list = Collections.synchronizedList(ll);

Related Articles:


Happy Coding !!
Happy Learning !!

Java - Vector class with example
Java - ArrayList class with example