BeginnerCoder

Lesson 13 — Linked list: reverse in place

Reverse a linked list

A classic linked-list skill: reverse the chain in place by turning every arrow around. Walk with three names: prev (already reversed part), node (current) and nxt (saved next node, so you do not lose the rest of the chain).

rest = cur.next     # 1. remember the rest
cur.next = before   # 2. turn the arrow around
before = cur        # 3. step forward
cur = rest

At the end, prev is the new head.

Why it matters: it is the standard test of whether you really understand pointers — and the same "save before you overwrite" care applies to all linked structures.

Want it another way?
Task

Write steps 2 and 3 from the theory inside the loop. Expected: 1 -> 2 -> 3 -> 4, then 4 -> 3 -> 2 -> 1.

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.