###### tags: `leetcode` # 206. Reverse Linked List My repo https://github.com/y56/proving-ground/tree/master/reverse-a-linked-list My note https://hackmd.io/ZqCf5c_BTz60oi4rHRrlxA?view ## A Recursive Version ![](https://www.geeksforgeeks.org/wp-content/uploads/2009/07/Linked-List-Rverse.gif) ```python= # Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None # recursively class Solution: def reverseList(self, head: ListNode) -> ListNode: if head is None or head.next is None: return head # Pass the new head, which was the tail tip. else: node = head # Just to change name to avoid confusing. tail_tip = self.reverseList(node.next) # Ask the next node to find the tail tip and tell me. (node.next).next = node node.next = None return tail_tip ``` OS stack has all the local variables to keep track of previous nodes. That's why we can do re-pointing from tail to head. Usually we need to use another variable to memorize the previous nodes for a singly linked list. ### wrong ```python= class Solution: def reverseList(self, head: ListNode) -> ListNode: if head is None or head.next is None: return head # Pass the new head, which was the tail tip. else: node = head # Just to change name to avoid confusing. (node.next).next = node node.next = None tail_tip = self.reverseList(node.next) return tail_tip ``` :::warning We must call the next level before doing the re-pointing. So as to re-point from tail to head. ::: ## An Iterative Version ![](https://media.geeksforgeeks.org/wp-content/cdn-uploads/RGIF2.gif) ```python= # Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None # iteratively class Solution: def reverseList(self, head: ListNode) -> ListNode: pre = None cur = head while cur: # Re-point them. nxt = cur.next cur.next = pre # Update them. pre = cur cur = nxt return pre # Important !!! Don't return cur ```