Reversing a Linked List

The classic interview question: turn 1 → 2 → 3 into 3 → 2 → 1 by flipping each arrow, with three pointers.

Python · intermediate · 8 min · 37 steps · free

What you'll learn

reverse_linked_list.py

class Node:
    def __init__(self, value, next=None):
        self.value = value
        self.next = next

def reverse(head):
    previous = None
    current = head
    while current is not None:
        following = current.next
        current.next = previous
        previous = current
        current = following
    return previous

head = Node(1, Node(2, Node(3)))
head = reverse(head)
print(head.value, head.next.value, head.next.next.value)

Step by step

  1. Python starts building the class Node. Its methods are stored, but none of them run yet.
  2. Python reads the class body: everything indented under class Node belongs to it.
  3. The method __init__ is stored in Node. It runs only when it is called on an object.
  4. The class Node is now defined, ready to make objects from.
  5. Python defines the function reverse(). Its body does not run yet: it runs each time the function is called.
  6. Python calls __init__(), with self = {}, value = 3 and next = null. It gets its own names.
  7. A new key value with the value 3 is added to the Node that self points to.
  8. A new key next with the value null is added to the Node that self points to.
  9. __init__() has finished. Python calls __init__(), with self = {}, value = 2 and next = {value: 3, next: null}. It gets its own names.
  10. A new key value with the value 2 is added to the Node that self points to.
  11. A new key next with the value a Node is added to the Node that self points to.
  12. __init__() has finished. Python calls __init__(), with self = {}, value = 1 and next = {value: 2, next: a Node}. It gets its own names.
  13. A new key value with the value 1 is added to the Node that self points to.
  14. A new key next with the value a Node is added to the Node that self points to.
  15. __init__() has finished, so its names are gone. The value it returned, {value: 1, next: a Node}, is stored in head.
  16. Python calls reverse(), with head = {value: 1, next: a Node}. It gets its own names.
  17. previous is created but does not point to any object yet (null).
  18. current is created and points to the same Node as head. No copy is made.
  19. current is not None is true, so the loop body runs.
  20. following remembers node 2 before the link is cut.
  21. Node 1 now points back to previous: None. It will be the new tail.
  22. previous moves up to node 1.
  23. current moves on to node 2.
  24. current is not None is true, so the loop body runs.
  25. following now points to a different Node: {value: 3, next: null}.
  26. Node 2 now points back to node 1.
  27. previous now points to the same Node as current.
  28. current now points to the same Node as following.
  29. current is not None is true, so the loop body runs.
  30. following is set to null, so it no longer points to any object.
  31. next in the Node that current points to changes from null to a Node.
  32. previous now points to the same Node as current.
  33. current is set to null, so it no longer points to any object.
  34. current is not None is false, so the loop ends.
  35. previous is the new head: 3 → 2 → 1.
  36. reverse() has finished, so its names are gone. The value it returned, {value: 3, next: a Node}, is stored in head.
  37. It prints 3 2 1.

Common mistakes

Best practices

Key takeaways

Check yourself

Extra memory used?

Answer: O(1). Just three pointers.

What does previous start as?

Answer: None

·

More in Lists, Stacks, Queues and Hashing