VISUALGO//PROBLEMS
reverse linked list · linked listeasy
LeetCode #206
list
1
→
2
→
3
→
4
→
5
ELEMENTS
head
Python
def reverseList(head: ListNode | None) -> ListNode | None:
    prev = None
    curr = head
    while curr:
        nxt = curr.next   # save the rest of the list
        curr.next = prev  # flip the link
        prev = curr
        curr = nxt

    return prev  # the old tail is the new head