Trace an in-place reversal

A singly linked list holds the values 4, 9, 2 and 7, in that order from the head, with the last node's link set to null. Every value is distinct, so you can name each node by its value. The list is reversed in place by this loop:


Plain text
prev = null
curr = head
while curr != null:
    nxt = curr.next
    curr.next = prev
    prev = curr
    curr = nxt
head = prev


For each pass through the loop body, fill in the value the link of the current node is set to, then the node that prev refers to and the node that curr refers to at the end of that pass. Write each node as its value, and write null for the empty reference.

Iterationcurr.next set toprev aftercurr after
1
2
3
4

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Singly Linked Lists Questions: