Skip to content

Instantly share code, notes, and snippets.

@claytonjwong
Created December 22, 2021 15:25
Show Gist options
  • Save claytonjwong/82e506cb4fda767cb9cc04f0a123dd22 to your computer and use it in GitHub Desktop.
Save claytonjwong/82e506cb4fda767cb9cc04f0a123dd22 to your computer and use it in GitHub Desktop.
reverse linked list
#
# Recursively change each node of the linked list such that next
# is the last node seen during a linear scan of the linked list.
#
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
def go(node = head, last = None):
next = node.next
node.next = last
if not next:
return node
return go(next, node)
return go() if head else None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment