LinkedList in Java

In Java, the LinkedList class is a part of the Java Collections Framework and implements several interfaces to provide a versatile, doubly-linked list structure.


🔧 LinkedList Implements:

Interface Purpose
List Allows ordered elements with duplicates
Deque Enables use as a double-ended queue (add/remove from both ends)
Queue Supports queue operations (FIFO)
Cloneable Allows cloning of LinkedList objects
Serializable Enables LinkedList to be serialized (written to files/streams)

📦 Class Declaration:

public class LinkedList<E> extends AbstractSequentialList<E>
        implements List<E>, Deque<E>, Cloneable, Serializable


🧰 Key Functionalities from Interfaces:

✅ From List:

  • Indexed access (get(int index))
  • Allows duplicates
  • Insertion order maintained

✅ From Deque:

  • addFirst(E e), addLast(E e)
  • removeFirst(), removeLast()
  • peekFirst(), peekLast()

✅ From Queue:

  • FIFO operations: offer(E e), poll(), peek()

🔍 Example Usage:

import java.util.LinkedList;

public class LinkedListDemo {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();

        list.add("Java");
        list.addFirst("Start");
        list.addLast("End");

        System.out.println(list);  // [Start, Java, End]

        list.removeFirst();        // Removes "Start"
        System.out.println(list.peekLast()); // Prints "End"
    }
}


🧠 Summary:

Interface Enables
List Indexed access, duplicates, insertion order
Deque Double-ended operations (stack & queue)
Queue FIFO behavior
Cloneable Object cloning
Serializable Object persistence (files/streams)


✅ Custom LinkedList:

Here’s how you can create a custom LinkedList implementation in Java from scratch (without using java.util.LinkedList):


✅ Custom Singly LinkedList Implementation

// Node class
class Node {
    int data;
    Node next;

    Node(int data) {
        this.data = data;
        this.next = null;
    }
}

// Custom LinkedList class
class MyLinkedList {
    Node head;

    // Add element at end
    public void add(int data) {
        Node newNode = new Node(data);

        if (head == null) {
            head = newNode;
            return;
        }

        Node temp = head;
        while (temp.next != null) {
            temp = temp.next;
        }

        temp.next = newNode;
    }

    // Print all elements
    public void printList() {
        Node temp = head;

        while (temp != null) {
            System.out.print(temp.data + " -> ");
            temp = temp.next;
        }

        System.out.println("null");
    }

    // Remove element by value
    public void remove(int key) {
        if (head == null) return;

        if (head.data == key) {
            head = head.next;
            return;
        }

        Node temp = head;
        while (temp.next != null && temp.next.data != key) {
            temp = temp.next;
        }

        if (temp.next != null) {
            temp.next = temp.next.next;
        }
    }
}

// Demo class
public class Main {
    public static void main(String[] args) {
        MyLinkedList list = new MyLinkedList();
        list.add(10);
        list.add(20);
        list.add(30);

        list.printList();  // 10 -> 20 -> 30 -> null

        list.remove(20);
        list.printList();  // 10 -> 30 -> null
    }
}


🧠 Key Concepts:

  • Node is a static inner class with data and next pointer.
  • MyLinkedList contains methods to:
    • Add at the end
    • Print the list
    • Remove by value

 

 Linked List Types:

 


🔗 1. Singly Linked List

Each node contains:

  • Data
  • Pointer to the next node

✅ Traversal: Only forward

[10] → [20] → [30] → null

Use-case:

  • Simple lists, stacks

🔁 2. Doubly Linked List

Each node contains:

  • Data
  • Pointer to next node
  • Pointer to previous node

✅ Traversal: Forward and backward

null ← [10] ⇄ [20] ⇄ [30] → null

Use-case:

  • Navigation systems, undo-redo operations

🔄 3. Circular Linked List

The last node points back to the first node.

Types:

  • Singly Circular: Last node points to head
  • Doubly Circular: Last’s next → head, head’s prev → last
[10] → [20] → [30] ─┐
  ↑                ↓
  └────────────────┘

Use-case:

  • Round-robin scheduling, multimedia playlist

🧠 Summary Table:

Type Traversal Extra Pointer Last Node Points To
Singly Linked List Forward only 1 (next) null
Doubly Linked List Forward & backward 2 (next, prev) null
Circular Linked List Forward or both 1 or 2 Head (forms a loop)
Back to blog

Leave a comment

Please note, comments need to be approved before they are published.