Java LinkedList class Example

By | 2018-03-23T19:26:07+05:30 June 11th, 2015|java|

In Java LinkedList class is a generic class it provides a linked-list data structure for storing and managing elements. This class implements List, Queue, and DeQueue interfaces.

Java LinkedList :

Some of the key features of the LinkedList class include:

  • LinkedList class is an implementation of doubly-linked list data structure, this allows operations to traverse the list from the beginning to end and from end to begining as required.

  • LinkedList has a capability to perform all List operations and store all types of elements including null and duplicate elements.

  • LinkedList has capability to maintain the insertion order.
  • Modification of the data inside LinkedList is fast when campared to ArrayList.
  • Capability to accept null elements.
  • LinkedList class is not synchronized.

Constructors in Java LinkedList :

  • LinkedList():Creates an empty LinkedList instance.

  • LinkedList(Collection<? extends E>c):Creates a LinkedList instance and initializes it with the elements in the collection c.

Methods in Java LinkedList :

The Java LinkedList class enables you to use the add(), get(), set(), remove(), and toArray() methods as like ArrayList.

In addition to that, the LinkedList class provides implementations for the methods in the Queue and DeQueue interfaces. These methods include:

  • E peek():Returns the first element in the linked list.

  • E poll():Returns the first element in the linked list and removes it from the list.

  • void addFirst(E e) and boolean offerFirst(E e):Insert the specified element to the start of the linked list.

  • void addLast(E e) and boolean offerLast(E e):Insert the specified element at the end of the linked list.

  • E getFirst() and E peekFirst():Return the first element in the linked list.

  • E getLast() and E peekLast():Return the last element in the linked list.

Note: The implementations provided by the ArrayList and LinkedList classes are not synchronized.

Basic Operations on Java LinkedList

package com.onlinetutorials;

import java.util.LinkedList;

public class LinkedListDemo {
    public static void main(String[] args) {
        LinkedList ll = new LinkedList();
        System.out.println("Elements in LinkedList : "+ll);
        // Add Element to LinkedList at First Place
        // Get the First Element from LinkedList
        System.out.println("First element : "+ ll.peek());
        // It gets the first element and removes from the LinkedList
        System.out.println("First element : "+ ll.poll());
        // Insert the elements in LinkedList at Last
        // get First element from LinkedList
        System.out.println("ll : "+ll);

Output :

Elements in LinkedList : [10, 200, 50, null] 
First element : 30 
First element : 30 
ll : [20, 10, 200, 50, null, Linked, List]

We can say that the above LinkedList can be represented in the memory like below.


If we add an element to LinkedList “ll”, each node will be created like above. And the each node will be points to the next node.

Example: 10 is pointing to 200 ,200 pointing to 50 and 50 is pointing to null.

Remove Elements from LinkedList :

If we want to delete an element from the LinkedList at middle, the node will be removed from the LinkedList and the pointers will point to the nearest node.

For example, if we want to remove 50 element from the LinkedList, we need to use remove() method on “ll” LinckedList instance like ll.remove(“50”);

LinkedListDeleteOn the above diagram representig the deletion of an element from linkedlist.

When do we use LinkedList ?

If our repeated operation is to insert or delete in the middle of the list, we can go with LinkedList instead of ArrayList.

Happy Learning 🙂

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Leave A Comment