Linked List
A treasure hunt where each clue says where the next one hides. To run it backwards you rewrite one clue at a time, keeping a finger on the next clue before you overwrite it. Reversing a list is the same: save next, flip, step on.
2 · Watch
No sound? Captions are on by default.
3 · Now you drive
Keys:←→ stepSpace playR reset
Reverse Linked List
or try
1–7 numbers from -99 to 99, separated by spaces.prev starts at nothing, curr at the head, 1.Step 1 of 17. Starting out. List: 1, 2, 3, 4, 5. prev at nothing, curr at 1.
Step 1 of 17
solution.ts▶ marks the line running now
function reverseList(head: ListNode | null): ListNode | null {
let prev: ListNode | null = null;
let curr = head;
while (curr !== null) {
const next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
}
4 · Your turn
0 of 6 problems solved
- Easy01
Reverse Linked List
The one you just watched. Write it from memory.
- Easy02
Merge Two Sorted Lists
Warm-up: walk both lists, always taking the smaller head.
- Easy03
Linked List Cycle
A fast pointer laps a slow one exactly when a loop exists.
- Medium04
Reorder List
Find the middle, reverse the back half, then weave.
- Medium05
Remove Nth Node From End of List
Two pointers, n apart, walk to the end together.
- Hard06
Merge K Sorted Lists
Merge pairs again and again until one list is left.