Iterative reversal of a singly linked list. Which body is correct?
prev, cur = None, head
while cur:
# body
return prevShow answer
Answer
nxt = cur.next; cur.next = prev; prev = cur; cur = nxt
Save next before overwriting it, then link back and advance. Overwriting cur.next first makes cur = cur.next jump to prev and loses the rest. Setting prev before relinking makes cur point to itself. The last option also links cur to itself because prev was already moved.