P
Primitive
A Cloudflare-native knowledge workspace for folders, topics, snippets, revisions, and file assets.
Reverse a singly linked list by maintaining three pointers: prev, curr, and next. Redirect each node to its predecessor, then advance the window. Runs in O(n) time with O(1) extra space.
def reverseList(head):
prev, curr = None, head
while curr:
nxt = curr.next
curr.next = prev
prev, curr = curr, nxt
return prev