Linked Lists teach one of the most important skills in DSA:
thinking in terms of pointers and references instead of indexes.
Most candidates fail linked list questions not because the logic is hard, but because they lose track of links.
This module builds that confidence step by step.
A singly linked list is a collection of nodes where:
Structure:
[data | next] → [data | next] → null
class Node {
int data;
Node next;
}
Node curr = head;
while(curr != null){
System.out.print(curr.data + " ");
curr = curr.next;
}
No random access.
Traversal is always O(n).
Each node stores:
Structure:
null ← [prev | data | next] ⇄ [prev | data | next] → null
prev pointerUsed in:
Last node points back to the head instead of null.
Structure:
head → node → node → head
Used in:
Use two pointers:
Fast pointer reaches end earlier, helping detect patterns.
Used for:
Node prev = null;
Node curr = head;
while(curr != null){
Node nextNode = curr.next;
curr.next = prev;
prev = curr;
curr = nextNode;
}
head = prev;
Node reverse(Node head){
if(head == null || head.next == null)
return head;
Node newHead = reverse(head.next);
head.next.next = head;
head.next = null;
return newHead;
}
Always store next before changing pointers.
Determine if a linked list contains a loop.
Node slow = head, fast = head;
while(fast != null && fast.next != null){
slow = slow.next;
fast = fast.next.next;
if(slow == fast) return true;
}
return false;
Time: O(n)
Space: O(1)
Fast & slow pointer.
Node slow = head, fast = head;
while(fast != null && fast.next != null){
slow = slow.next;
fast = fast.next.next;
}
Used in:
Merge nodes by comparing values.
Node dummy = new Node(0);
Node tail = dummy;
while(l1 != null && l2 != null){
if(l1.data < l2.data){
tail.next = l1;
l1 = l1.next;
} else {
tail.next = l2;
l2 = l2.next;
}
tail = tail.next;
}
tail.next = (l1 != null) ? l1 : l2;
Time: O(n + m)
Space: O(1)
Two linked lists intersect at some node.
Alternative:
Node a = headA, b = headB;
while(a != b){
a = (a == null) ? headB : a.next;
b = (b == null) ? headA : b.next;
}
return a;
Elegant and interview favorite.
Stores most recently used items and removes least recently used.
Operations:
This problem combines: