Skip to main content

πŸ”— Linked List

One-line summary: A dynamic chain of nodes β€” O(1) insert/delete at known position, O(n) access by index. Master reversal, cycle detection, and merge patterns.


Concept​

Each node: val + next pointer (singly), or prev+next (doubly).

Types: Singly, Doubly, Circular.


Diagram​

Linked List Structure Linked List GIF


Time & Space Complexity​

OperationTimeSpace
Access by indexO(n)O(1)
Insert/Delete at headO(1)O(1)
ReverseO(n)O(1)
Cycle detection (Floyd's)O(n)O(1)
Find middleO(n)O(1)

Common Patterns​

Reverse (Iterative)​

function reverseList(head) {
let prev = null, curr = head;
while (curr) { const next = curr.next; curr.next = prev; prev = curr; curr = next; }
return prev;
}

Cycle Detection (Floyd's)​

function hasCycle(head) {
let slow = head, fast = head;
while (fast && fast.next) {
slow = slow.next; fast = fast.next.next;
if (slow === fast) return true;
}
return false;
}

Find Middle​

function findMiddle(head) {
let slow = head, fast = head;
while (fast && fast.next) { slow = slow.next; fast = fast.next.next; }
return slow;
}

Merge Two Sorted Lists​

function mergeTwoLists(l1, l2) {
const dummy = { next: null }; let cur = dummy;
while (l1 && l2) {
if (l1.val <= l2.val) { cur.next = l1; l1 = l1.next; }
else { cur.next = l2; l2 = l2.next; }
cur = cur.next;
}
cur.next = l1 || l2;
return dummy.next;
}

Pitfalls​

  • Losing reference to next before reassigning β€” store it first
  • Forgetting to update tail.next = null after reversal
  • Off-by-one in middle finding: even-length list has two middles

Practice Problems​

ProblemDifficultySolution
LC 206 β€” Reverse Linked ListEasy
LC 160 β€” Intersection of Two Linked ListsEasy
LC 141 β€” Linked List CycleEasy
LC 876 β€” Middle of the Linked ListEasy
LC 19 β€” Remove Nth Node From EndMedium
LC 143 β€” Reorder ListMedium
LC 234 β€” Palindrome Linked ListEasy
LC 25 β€” Reverse Nodes in k-GroupHard
LC 138 β€” Copy List with Random PointerMedium
CC β€” Linked List Operations (LISTOPS)Medium
LC 2 β€” Add Two NumbersMedium
LC 21 β€” Merge Two Sorted ListsEasy
LC 83 β€” Remove Duplicates from Sorted ListEasy
LC 203 β€” Remove Linked List ElementsEasy
LC 237 β€” Delete Node in a Linked ListEasy
LC 1290 β€” Convert Binary Number in a Linked List to IntegerEasy
LC 1474 β€” Delete N Nodes After M Nodes of a Linked ListEasy
LC 24 β€” Swap Nodes in PairsMedium
LC 61 β€” Rotate ListMedium
LC 82 β€” Remove Duplicates from Sorted List IIMedium
LC 86 β€” Partition ListMedium
LC 92 β€” Reverse Linked List IIMedium
LC 142 β€” Linked List Cycle IIMedium
LC 147 β€” Insertion Sort ListMedium
LC 148 β€” Sort ListMedium
LC 328 β€” Odd Even Linked ListMedium
LC 445 β€” Add Two Numbers IIMedium
LC 725 β€” Split Linked List in PartsMedium
LC 817 β€” Linked List ComponentsMedium
LC 1019 β€” Next Greater Node In Linked ListMedium
LC 1367 β€” Linked List in Binary TreeMedium
LC 1721 β€” Swapping Nodes in a Linked ListMedium
CC β€” Linked List Operations (LISTOPS)Medium
CC β€” Reverse Linked List (REVLIST)Medium
CC β€” Merge Sorted Lists (MERGESORT)Medium
LC 23 β€” Merge k Sorted ListsHard
LC 146 β€” LRU CacheHard
LC 460 β€” LFU CacheHard
LC 1206 β€” Design SkiplistHard
CC β€” Advanced Linked List (ADVLIST)Hard

  • Two Pointers β€” fast/slow is a linked-list technique
  • Stack β€” LRU = doubly linked list + hash map
  • Trees β€” tree nodes are linked nodes with more pointers

← Back to Home Β· Β© sparshjaswal