while head: # hold the next one, and reassign the prev one as current node's next next, head.next = head.next, prev # hold the current node as the prev and the next as current prev, head = head, next
# the loop above ends with None as the head so the prev is the tail node return prev