Reverse Linked List

LeetCode

LeetCode: https://leetcode.com/problems/reverse-linked-list/

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

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
cur
1
0
2
1
3
2
4
3
5
4
prevIdx=null
reversed=[]
remaining=[1, 2, 3, 4, 5]
Step 1 / 6
Step 1:
Initialize prev=nil, cur=head. No links reversed yet.
Pointers: cur=0
Focus: select @ [0]