Skip to content

Reverse Linked List

LeetCode

01 · Question

Given the head of a singly linked list, reverse the list, and return the reversed list.

02 · Solution

Reference solution

1def reverseList(head: Optional[ListNode]) -> Optional[ListNode]:
2 prev = None
3 cur = head
4
5 while cur:
6 nxt = cur.next
7 cur.next = prev
8 prev = cur
9 cur = nxt
10
11 return prev