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
| Feature | Array | Linked List |
|---|---|---|
| Memory | Contiguous | Non-contiguous |
| Access by Index | O(1) | O(n) |
| Insert at Beginning | O(n) | O(1) |
| Delete at Beginning | O(n) | O(1) |
| Dynamic Size | No | Yes |
| Extra Memory | No | Yes (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
| Operation | Singly LL |
|---|---|
| Traverse | O(n) |
| Search | O(n) |
| Insert Beginning | O(1) |
| Insert End | O(n) |
| Delete Beginning | O(1) |
| Delete End | O(n) |
| Reverse | O(n) |
| Find Middle | O(n) |
22. Common Interview Problems
Easy
- Traverse Linked List
- Search Element
- Length of List
- Reverse Linked List
- Find Middle Node
Medium
- Detect Cycle
- Remove Cycle
- Merge Sorted Lists
- Nth Node From End
- Palindrome Linked List
Hard
- Reverse Nodes in K Groups
- Merge K Sorted Lists
- LRU Cache
- Copy List with Random Pointer
- Intersection of Two Linked Lists
23. Revision Summary
Linked List:
- Dynamic Data Structure
- Nodes connected via references
Types:
- Singly Linked List
- Doubly Linked List
- Circular Linked List
- 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:
- Reverse Linked List
- Middle Node
- Detect Cycle
- Merge Two Sorted Lists
- Nth Node From End
- Reverse in K Groups
- LRU Cache
Recommended Practice Order
- Create Singly Linked List
- Insert Operations
- Delete Operations
- Search
- Reverse Linked List
- Find Middle Node
- Detect Cycle
- Doubly Linked List
- Circular Linked List
- Merge Sorted Lists
- Nth Node From End
- Reverse in K Groups