Reverse Linked List
https://leetcode.com/problems/reverse-linked-list/
prev = None
curr = head
while curr:
next_temp = curr.next
curr.next = prev
prev = curr
curr = next_temp
return prevA shorter version
prev, curr = None, head
while curr:
curr.next, prev, curr = prev, curr, curr.nextThere’s not need to use a stack!
Alternatively, use recursion.
if not head or not head.next:
return head
p = self.reverseList(head.next)
head.next.next = head
head.next = None
return p