1. Concept library
  2. Data Structures & Algorithms
  3. Module 4: Linked lists

Concept

Reversing a list

Reversing a linked list in place means walking it once and flipping each next pointer to point backwards. Three variables, previous, current and next, are all it takes.

What a session covers

The tutor works through these with you, in conversation and code. A session ends when you have shown them, not when a video does.

  1. Explain the three-variable walk that reverses a list in place
  2. Write the reversal and trace it on a list of four nodes
  3. Spot the bug of returning head instead of previous

In practice

A few lines that show Reversing a list doing its job.

Python
def reverse(head):
    previous = None
    current = head
    while current:
        following = current.next     # save before overwriting
        current.next = previous
        previous = current
        current = following
    return previous                  # the new head

The common mistake

Watch for

Returning head instead of previous. After the loop, head is the old first node, now the tail, and previous holds the new head.

What to know first

Your roadmap puts these before Reversing a list, unless you already know them.

How it stays learned

Once a session shows you understand Reversing a list, it comes back for review. Each review takes a minute and tells the memory model how well you still know it.

  1. Day 3First recall, while it is fresh
  2. Day 7Second, a week on
  3. Day 18Last, when it has settled
Previous in the module← Fast and slow pointers