Sign inEnroll free
DSA Visualizer · 25 visualizationsReverse Linked List
EasyLinked List

Reverse Linked List

Given the head of a singly linked list, reverse the list, and return the reversed list. The key insight is to use three pointers: prev, curr, and next to iteratively reverse each link.

The step-through player is coming for this one. Two Sum has the full animation today; the rest are being authored in the admin.
Pythontime O(n) · space O(1)
def reverseList(head):
    prev, curr = None, head
    while curr:
        next_node = curr.next  # save next
        curr.next = prev       # reverse pointer
        prev = curr            # advance prev
        curr = next_node       # advance curr
    return prev
Two Sum is free. Enroll to unlock every visualization and save where you stopped.Enroll free
Watch the video walkthrough