Linked List
Reach for it when the problem says
- Reverse
- merge
- find the middle
- detect a cycle
- Solved
- 0/31Solved
- Problems
- 31Problems
- Variants
- 5Variants
Problems
5 variantsIn-place reversal
0 of 7 solved- Reverse a Linked ListUp nextEssentialsDeep diveEasyDecode Reverse a Linked List
- Palindrome Linked ListEasyDecode Palindrome Linked List
- Reorder ListDeep diveMediumDecode Reorder List
- Reverse Linked List IISprintDeep diveMediumDecode Reverse Linked List II
- Swap Nodes in PairsMediumDecode Swap Nodes in Pairs
- Maximum Twin Sum of a Linked ListEssentialsDeep diveMediumDecode Maximum Twin Sum of a Linked List
- Reverse Nodes in k-GroupSprintDeep diveHardDecode Reverse Nodes in k-Group
Merging and combining
0 of 6 solved- Merge Two Sorted ListsSprintDeep diveEasyDecode Merge Two Sorted Lists
- Add Two NumbersSprintDeep diveMediumDecode Add Two Numbers
- Sort ListSprintDeep diveMediumDecode Sort List
- Insertion Sort ListMediumDecode Insertion Sort List
- Add Two Numbers IIMediumDecode Add Two Numbers II
- Merge k Sorted ListsSprintDeep diveHardDecode Merge k Sorted Lists
Fast and slow pointers
0 of 7 solved- Middle of the ListDeep diveEasyDecode Middle of the List
- Detect a CycleSprintDeep diveEasyDecode Detect a Cycle
- Intersection of Two Linked ListsEasyDecode Intersection of Two Linked Lists
- Remove the Nth Node From the EndSprintDeep diveMediumDecode Remove the Nth Node From the End
- Delete the Middle Node of a Linked ListEssentialsDeep diveMediumDecode Delete the Middle Node of a Linked List
- Linked List Cycle IIMediumDecode Linked List Cycle II
- Find the Duplicate NumberMediumDecode Find the Duplicate Number
Dummy node and relinking
0 of 8 solved- Remove Linked List ElementsEasyDecode Remove Linked List Elements
- Remove Duplicates from Sorted ListEasyDecode Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List IISprintDeep diveMediumDecode Remove Duplicates from Sorted List II
- Odd Even Linked ListEssentialsDeep diveMediumDecode Odd Even Linked List
- Partition ListSprintDeep diveMediumDecode Partition List
- Rotate ListSprintDeep diveMediumDecode Rotate List
- Split Linked List in PartsMediumDecode Split Linked List in Parts
- Populating Next Right Pointers in Each Node IISprintDeep diveMediumDecode Populating Next Right Pointers in Each Node II
Design and copying
0 of 3 solved- Copy List with Random PointerSprintDeep diveMediumDecode Copy List with Random Pointer
- LRU CacheSprintDeep diveMediumDecode LRU Cache
- Design Browser HistoryMediumDecode Design Browser History
Pattern guide
Linked list questions show up in almost every interview loop because they test one thing very directly: can you change pointers without losing track of the data. The algorithms are short. The bugs are always the same few, a lost next, a missing dummy node, a null dereference at the end, so the people who do well are the ones who recognise the move early and use a template they trust.
What it is
A linked list is a chain of nodes. Each node holds a value and a next pointer to the node after it, and the last node points to nothing (None in Python, null in TypeScript). You only hold the head. There is no index: to reach the fifth node you walk four next pointers.
That one limitation shapes every technique on this page. You cannot step backwards, you cannot jump to the middle, and you cannot ask for the length without walking. What you can do cheaply is change a pointer: inserting or removing a node anywhere is O(1) once you are standing on the node before it.
Here is the most important move, reversing a list in place. Watch the three pointers. nxt saves the rest of the list before curr.next is overwritten.
Start with prev = None and curr on the head. Nothing is reversed yet.
Every node was visited once and only three variables were used, so this is O(n) time and O(1) space. Most linked list problems are a combination of this and a handful of other moves.
curr.next = prev before saving curr.next?How to recognise it
Wording clues
| Phrase in the prompt | What it implies |
|---|---|
| "middle node", "halfway" | Fast and slow pointers: fast moves two, slow moves one. |
| "cycle", "loop", "never ends" | Floyd's cycle detection, and phase two if they want the entry node. |
| "nth from the end", "kth from the end" | Two pointers with a fixed gap of n. |
| "reverse", "in pairs", "k at a time", "between positions" | In-place reversal of all or part of the list. |
| "palindrome", "twin", "front and back" | Find the middle, reverse the back half, compare. |
| "remove all", "delete", "the head may be removed" | A dummy node before the head. |
| "sorted lists", "merge", "k lists" | Merge with a dummy and a tail; a heap when there are k lists. |
| "in place", "O(1) extra space" | No arrays: relink the nodes. |
| "least recently used", "back and forward" | A design problem built on a doubly linked list. |
| "deep copy", "random pointer" | A hash map from original node to copy. |
Constraint clues
| Constraint | What it suggests |
|---|---|
0 <= number of nodes | The empty list is a real input. Check head before touching head.next. |
Up to 10^4 or 10^5 nodes | O(n) is the target; repeatedly walking to the tail is O(n²). |
k up to 2 * 10^9 | Reduce k modulo the length before you walk anything. |
| "Values may repeat" or small value ranges | Compare nodes by identity (is, ===), never by value. |
| "O(1) average time" per operation | A hash map plus a doubly linked list. |
Input and output shape clues
| Shape | What it suggests |
|---|---|
You get one head and return a head | Relinking. The head may change, so think about a dummy node. |
| You get two heads | Walk them together: merging, adding digits, or finding where they join. |
| You return a node (not a list) | The tests check identity: the middle node, the cycle entry, the intersection. |
| You return nothing | The change happens in place from the original head. |
| You get an array where every value is a valid index | It is secretly a linked list: i points to nums[i]. |
Looks like it but isn't
- Two pointers on an array. Converging pointers need to step backwards. On a singly linked list they cannot, so you either reverse half the list or copy it into an array (O(n) space).
- Stack. A stack reverses order for free, which is why it is tempting for palindromes and reordering. It works, but costs O(n) memory that an in-place reversal avoids. Reach for a stack when you must not modify the input, as in Add Two Numbers II.
- Graphs. A cycle in a list sounds like graph cycle detection, but each node has exactly one outgoing edge, so Floyd's two pointers replace DFS and a visited set.
- Heap. A heap only earns its place when you have k sorted lists. For two lists a single comparison is enough.
p.val == q.val. Why is that wrong?The sub-patterns
Fast and slow pointers
Use it when you need a position you cannot index: the middle, the nth node from the end, or whether the list loops. Two pointers walk at different speeds (one and two steps) or with a fixed gap. The faster one measures the list for you, so you never count the length first.
Both pointers start on the head.
Press Run (⌘ Enter) to see what happens.Press Run (⌘ Enter) to see what happens.Complexity: O(n) time, O(1) space. Best-known problems: Middle of the Linked List, Linked List Cycle, Linked List Cycle II, Remove Nth Node From End of List, Find the Duplicate Number.
What does this print?
values = [10, 20, 30, 40, 50, 60] slow = fast = 0 while fast < len(values) - 1: slow += 1 fast = min(fast + 2, len(values)) print(values[slow])
In-place reversal
Use it when the answer needs nodes in reverse order: the whole list, a stretch between two positions, groups of k, or the back half so you can compare it with the front (palindromes, twin sums, reorder). The template is always the same three-pointer loop. What changes is where you start and where you stop.
Cut the list after the first half so you have two separate chains.
Press Run (⌘ Enter) to see what happens.Press Run (⌘ Enter) to see what happens.Starting prev at stop instead of None is the trick for partial reversals: the reversed piece's new tail is already linked to whatever comes after it, which is exactly what Reverse Nodes in k-Group needs.
Complexity: O(n) time, O(1) space. Best-known problems: Reverse Linked List, Reverse Linked List II, Reverse Nodes in k-Group, Palindrome Linked List, Reorder List.
Dummy node and relinking
Use it whenever the head might change: removing nodes by value, cutting out runs of duplicates, partitioning around a value, rotating, splitting into parts. A dummy (or sentinel) node is a throwaway node placed before the head. Every real node now has a predecessor, so deleting the head is the same code as deleting any other node, and you return dummy.next at the end.
Put a dummy D before the head and start curr on it.
Press Run (⌘ Enter) to see what happens.Press Run (⌘ Enter) to see what happens.Partition List and Odd Even Linked List use two dummies: build two chains as you walk, then join them. Remember to end the second chain with None, or its old next can make a cycle.
Complexity: O(n) time, O(1) space. Best-known problems: Remove Linked List Elements, Remove Duplicates from Sorted List II, Partition List, Odd Even Linked List, Rotate List.
Merging and combining
Use it when you walk two or more lists together: merging sorted lists, adding numbers stored as digits, sorting a list (merge sort is split plus merge). Keep a dummy node and a tail pointer, append one node per step, and attach the leftover at the end. With k lists, a min-heap of the k front nodes picks the next one in O(log k).
Two sorted lists, a dummy D, and tail on the dummy.
Press Run (⌘ Enter) to see what happens.Press Run (⌘ Enter) to see what happens.Complexity: O(n + m) for two lists; O(N log k) for k lists with N nodes in total. Best-known problems: Merge Two Sorted Lists, Merge k Sorted Lists, Add Two Numbers, Sort List.
Design and copying
Use it when the problem asks you to build a structure, not transform a list: an LRU cache, browser history, or a deep copy of a list with extra pointers. The linked list is a building block here. It is usually doubly linked (each node has prev and next), so any node can be unlinked in O(1), and it is often paired with a hash map that finds nodes by key.
get(key) looks the node up in the map. Without the map you would have to walk the list.
Press Run (⌘ Enter) to see what happens.Press Run (⌘ Enter) to see what happens.For copying, the same pairing appears: a map from each original node to its copy lets you wire next and random in a second pass, even when random points ahead to a node you have not copied yet.
Complexity: O(1) per cache operation; O(n) time and space for a deep copy. Best-known problems: LRU Cache, Copy List with Random Pointer, Design Browser History.
A decision checklist
When you read a linked list prompt, ask these in order: do I need a position (middle, end, loop)? Do I need reverse order? Can the head change? Am I walking several lists? Am I building a structure? The first yes usually names the sub-pattern.
Match each problem to the sub-pattern that cracks it.
Put the steps of Reorder List (L0, Ln, L1, Ln-1, ...) in order.
- Tap the steps below in the right order.
Traps
- Losing the rest of the list. Save
nxt = curr.nextbefore you overwritecurr.next. This is the most common linked list bug. - Null dereference at the end. In a fast pointer loop, check
fast and fast.nextbefore readingfast.next.next. Checkheadbefore readinghead.next. - Forgetting the head can change. If a node you remove or move might be the head, use a dummy node. Without one you need a separate branch, and that branch is where bugs hide.
- Moving on after an unlink. When removing nodes, stay on
curraftercurr.next = curr.next.next; the newcurr.nextmay also need removing. - Leaving a stale link. When you build chains by appending (partition, odd/even, split), the last node still points into the old list. Set its
nexttoNone, or you create a cycle. - Comparing values instead of nodes. For cycles and intersections, the question is about identity. Use
is/===. - Off-by-one in the middle.
fast = headlands on the second middle for even lengths;fast = head.nextlands on the first. Pick on purpose: deleting needs the node before the middle, and merge sort needs the first middle so a two-node list splits evenly.
Always test these inputs: an empty list, one node, two nodes, an odd and an even length, and a case where the head is the node that changes.
1 → 4 → 2 around 3 into a small chain 1 → 2 and a big chain 4, then join them. You forget to set the big tail's next to None. What happens?How to talk it through
- Dummy node
- A throwaway node placed before the head so every real node has a predecessor. Return
dummy.nextat the end. - Fast and slow pointers
- Two pointers moving at one and two steps (or with a fixed gap). They find the middle, the nth node from the end, and cycles in one pass.
- Floyd's cycle detection
- Fast and slow pointers meet inside a loop; then a pointer from the head and one from the meeting point meet at the loop's entry.
- In-place reversal
- Flip
nextpointers withprev,currand a savednxt, using O(1) extra space. - Doubly linked list
- Each node has
prevandnext, so any node can be unlinked in O(1). The base of an LRU cache. - Deep copy
- A copy made of new nodes whose pointers all point into the copy, never back into the original.