Linked List

Singly, doubly, circular — operations and interview patterns


1. Introduction

A Linked List is a linear data structure where elements are stored in separate memory locations and connected using references (pointers).

Unlike arrays, linked list elements are not stored in contiguous memory locations.

Example:

10 -> 20 -> 30 -> 40 -> NULL

Each element is called a Node.


2. Why Linked List?

Arrays have fixed sizes and insertion/deletion in the middle requires shifting elements.

Example:

10 20 30 40

Insert 15 after 10:

10 15 20 30 40

All remaining elements shift.

Time Complexity:

O(n)

Linked List:

10 -> 20 -> 30 -> 40

Insert 15:

10 -> 15 -> 20 -> 30 -> 40

Only links change.


3. Array vs Linked List

FeatureArrayLinked List
MemoryContiguousNon-contiguous
Access by IndexO(1)O(n)
Insert at BeginningO(n)O(1)
Delete at BeginningO(n)O(1)
Dynamic SizeNoYes
Extra MemoryNoYes (pointer)

4. Node Structure

A node consists of:

+---------+---------+
|  Data   |  Next   |
+---------+---------+

Example:

+------+------+
| 10   |  *---|
+------+------+

Java Node Class

class Node {
    int data;
    Node next;

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

5. Head Pointer

The first node of a linked list is accessed through a pointer called Head.

head
 |
 v

10 -> 20 -> 30 -> NULL

Without the head pointer, the list cannot be accessed.


6. Traversal

Traversal means visiting every node.

Example:

10 -> 20 -> 30 -> 40

Code:

Node current = head;

while(current != null){
    System.out.println(current.data);
    current = current.next;
}

Output:

10
20
30
40

Complexity:

O(n)

7. Singly Linked List

Each node points to only one next node.

10 -> 20 -> 30 -> NULL

Traversal is possible only in forward direction.


8. Insertion Operations


Insert at Beginning

Before:

10 -> 20 -> 30

After inserting 5:

5 -> 10 -> 20 -> 30

Code:

newNode.next = head;
head = newNode;

Complexity:

O(1)

Insert at End

Before:

10 -> 20 -> 30

After inserting 40:

10 -> 20 -> 30 -> 40

Code:

Node temp = head;

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

temp.next = newNode;

Complexity:

O(n)

With Tail Pointer:

O(1)

Insert at Position

Before:

10 -> 20 -> 40

Insert 30 at position 3:

10 -> 20 -> 30 -> 40

Code:

newNode.next = temp.next;
temp.next = newNode;

Complexity:

O(n)

9. Deletion Operations


Delete at Beginning

Before:

10 -> 20 -> 30

After:

20 -> 30

Code:

head = head.next;

Complexity:

O(1)

Delete at End

Before:

10 -> 20 -> 30

After:

10 -> 20

Need to reach second-last node.

Complexity:

O(n)

Delete by Value

Delete:

20

Before:

10 -> 20 -> 30

After:

10 -> 30

Complexity:

O(n)

10. Search Operation

Find:

30

Code:

Node current = head;

while(current != null){

    if(current.data == 30)
        return true;

    current = current.next;
}

return false;

Complexity:

O(n)

11. Length of Linked List

Code:

int count = 0;

Node current = head;

while(current != null){
    count++;
    current = current.next;
}

Complexity:

O(n)

12. Reverse Linked List

Most frequently asked interview problem.

Before:

10 -> 20 -> 30 -> NULL

After:

30 -> 20 -> 10 -> NULL

Three Pointer Method

Node prev = null;
Node curr = head;

while(curr != null){

    Node next = curr.next;

    curr.next = prev;

    prev = curr;

    curr = next;
}

head = prev;

Complexity:

O(n)

Space:

O(1)

13. Find Middle Node

Example:

10 -> 20 -> 30 -> 40 -> 50

Middle:

30

Fast and Slow Pointer

Node slow = head;
Node fast = head;

while(fast != null && fast.next != null){

    slow = slow.next;
    fast = fast.next.next;
}

Answer:

slow

Complexity:

O(n)

14. Cycle Detection

Example:

10 -> 20 -> 30 -> 40
      ^         |
      |_________|

A cycle exists.


Floyd's Cycle Detection Algorithm

Also called:

Tortoise and Hare Algorithm

Code:

Node slow = head;
Node fast = head;

while(fast != null && fast.next != null){

    slow = slow.next;
    fast = fast.next.next;

    if(slow == fast)
        return true;
}

return false;

Complexity:

O(n)

Space:

O(1)

15. Find Nth Node from End

Example:

10 -> 20 -> 30 -> 40 -> 50

2nd node from end:

40

Two Pointer Technique

Move first pointer N steps.

Then move both together.

When first reaches NULL:

second pointer = answer

Complexity:

O(n)

16. Merge Two Sorted Lists

List 1:

1 -> 3 -> 5

List 2:

2 -> 4 -> 6

Result:

1 -> 2 -> 3 -> 4 -> 5 -> 6

Technique:

Dummy Node

Complexity:

O(n + m)

17. Doubly Linked List

Each node contains:

Previous Pointer
Data
Next Pointer

Structure:

NULL <- 10 <-> 20 <-> 30 -> NULL

Node Structure

class Node{
    int data;
    Node prev;
    Node next;
}

Advantages

  • Forward traversal
  • Backward traversal
  • Easier deletion

Disadvantages

  • Extra memory required
  • More pointer manipulation

18. Circular Linked List

The last node points back to the first node.

10 -> 20 -> 30
^            |
|____________|

No NULL pointer exists.


Applications

  • Round Robin Scheduling
  • Multiplayer Games
  • Music Playlist
  • CPU Scheduling

19. Circular Doubly Linked List

Structure:

10 <-> 20 <-> 30
^              |
|______________|

Features:

  • Circular
  • Bidirectional Traversal

20. Important Interview Patterns


Fast and Slow Pointer

Used for:

  • Finding Middle Node
  • Detecting Cycle
  • Palindrome Linked List

Two Pointer Technique

Used for:

  • Nth Node from End
  • Intersection Problems

Dummy Node Pattern

Used for:

  • Merge Sorted Lists
  • Reverse in Groups
  • Delete Operations

21. Time Complexity Summary

OperationSingly LL
TraverseO(n)
SearchO(n)
Insert BeginningO(1)
Insert EndO(n)
Delete BeginningO(1)
Delete EndO(n)
ReverseO(n)
Find MiddleO(n)

22. Common Interview Problems

Easy

  1. Traverse Linked List
  2. Search Element
  3. Length of List
  4. Reverse Linked List
  5. Find Middle Node

Medium

  1. Detect Cycle
  2. Remove Cycle
  3. Merge Sorted Lists
  4. Nth Node From End
  5. Palindrome Linked List

Hard

  1. Reverse Nodes in K Groups
  2. Merge K Sorted Lists
  3. LRU Cache
  4. Copy List with Random Pointer
  5. Intersection of Two Linked Lists

23. Revision Summary

Linked List:

  • Dynamic Data Structure
  • Nodes connected via references

Types:

  1. Singly Linked List
  2. Doubly Linked List
  3. Circular Linked List
  4. Circular Doubly Linked List

Must Know Operations:

  • Traversal
  • Insert
  • Delete
  • Search
  • Reverse

Must Know Interview Algorithms:

  • Fast & Slow Pointer
  • Floyd Cycle Detection
  • Two Pointer Technique
  • Dummy Node Pattern

Most Important Interview Questions:

  1. Reverse Linked List
  2. Middle Node
  3. Detect Cycle
  4. Merge Two Sorted Lists
  5. Nth Node From End
  6. Reverse in K Groups
  7. LRU Cache

  1. Create Singly Linked List
  2. Insert Operations
  3. Delete Operations
  4. Search
  5. Reverse Linked List
  6. Find Middle Node
  7. Detect Cycle
  8. Doubly Linked List
  9. Circular Linked List
  10. Merge Sorted Lists
  11. Nth Node From End
  12. Reverse in K Groups