Reverse Linked List in Python

A really fancy way to reverse a linked list in Python:

1
2
3
4
5
def reverseList(head):
new_head = None
while head:
head.next, head, new_head = new_head, head.next, head
return new_head