List Fundamentals
What is this?
A linked list is a chain of nodes, where each node points to the next one โ think of a treasure hunt where each clue leads you to the following clue. The fundamentals are the basic hands-on moves for working with that chain: flipping all the arrows to run it backwards, building a brand-new chain as you walk, and rearranging the nodes in place.
๐ก Fun fact: Reversing a linked list is one of the most-asked coding interview questions of all time, yet the clean iterative solution needs only three pointer variables and a single pass through the list.
๐ The 3 problems in this chapter are free. Sign in with Google or Microsoft to start solving.
Core idea: Everything you'll ever do to a linked list is re-pointing
nextfields, in order, without losing the rest of the chain. This chapter drills the three moves that underpin the whole topic: reverse it, build a new one as you walk, and restructure it in place.
The two habits that prevent every bug
- Cache
nextbefore you overwrite it. A node has exactly one forward pointer; the instant you rewrite it, the rest of the list is gone unless you saved it. - Use a dummy head. A fake node sitting before
headmeans "edit/insert/delete at the front" stops being a special case โ you always have aprevto splice from.
Master those and pointer surgery becomes mechanical.
The three problems
- Reverse Linked List โ the keystone. Walk once, flipping each
nextto point at the previous node (cache-next first). O(n) time, O(1) space. Reorder and many later problems reuse this exact routine. - Add Two Numbers โ two digit-lists (least-significant first); walk both, sum digit + carry, append to a new list built behind a dummy head. The reverse digit order is why you can add left-to-right.
- Reorder List โ
L0โLnโL1โLn-1โโฆin place. Not one trick but three composed: find the middle (fast/slow), reverse the second half, merge the two halves alternately.
The moves this chapter teaches
| Move | Mechanic | Reused in |
|---|---|---|
| Cache-next reverse | save next, flip pointer, advance |
Reverse, Reorder |
| Dummy head + tail build | append to a fake-headed list | Add Two Numbers |
| Carry propagation | digit, carry = divmod(a+b+carry, 10) |
Add Two Numbers |
| Compose primitives | middle โ reverse โ merge | Reorder |
The progression is deliberate: Reverse teaches the atomic move, Add Two Numbers teaches building + dummy head, and Reorder shows that hard list problems are usually two or three easy ones glued together.
๐ Draw it yourself
- Reverse, arrow by arrow. Draw
1โ2โ3, then redraw it 3 times flipping one arrow per step withprev/curr/nextlabels. - Fold the tail onto the head. Write
1 2 3 4 5 6on a paper strip and fold it so6lands under1,5under2โ that's Reorder.
Snap photos and embed them with the /host-diagrams skill.
Key takeaways
- Pointer surgery, two rules: cache
nextbefore rewiring; use a dummy head for front edits. - Reverse Linked List is the keystone โ internalize it; it recurs everywhere.
- Build with a dummy head + carry for "construct a new list" problems (Add Two Numbers).
- Hard list problems decompose โ Reorder = find-middle + reverse + merge.
- Why it matters: these are the precision drills every harder list (and many tree/stream) problems are built from.
Order: Reverse Linked List โ Add Two Numbers โ Reorder List.
Add Two Numbers
Core idea: Add the two lists exactly the way you add on paper โ column by column from the ones place up โ letting a single
carryripple forward, and build the answer behind a dummy head so you never special-case the first digit.
Problem, rephrased
Imagine you're writing the addition unit for a calculator that has to handle numbers far bigger than any built-in integer type โ think a 10,000-digit balance in a ledger system. You can't load the whole number into a register, so each number arrives as a stream of single digits, one digit per node in a linked list.
There's one twist that turns out to be a gift: the digits are stored in reverse order, ones digit first. So the number 342 is stored as 3 -> 4 -> 2 (the 3 is the ones place, the 4 is the tens, the 2 is the hundreds). Read the list left to right and you're reading the number backwards.
Your job: given two such lists, return their sum as a third list in the same reverse-order format.
Why is reverse order a gift? Because when you add by hand, you start at the rightmost digit (the ones place) and carry leftward. Reverse storage puts that rightmost digit at the head of the list โ exactly where a linked list lets you start cheaply. The data is pre-arranged in the order you need to process it.
l1 (a number) |
l2 (a number) |
Output list | Means |
|---|---|---|---|
2 -> 4 -> 3 |
5 -> 6 -> 4 |
7 -> 0 -> 8 |
342 + 465 = 807 |
0 |
0 |
0 |
0 + 0 = 0 |
9 -> 9 |
1 |
0 -> 0 -> 1 |
99 + 1 = 100 |
Read the first row backwards: 2->4->3 is 342, 5->6->4 is 465, and 7->0->8 is 807. The sum is correct, just stored ones-first.
Sign in to continue reading
The rest of this lesson is available with a free account. Signing in with Google or Microsoft is free.
Sign in to read the full lesson