Getting everything ready for you…

Loading courses, problems and your progress. This takes just a moment.

0s

Visualizer › Linked List

Reverse a Linked List

Walks the list once with three pointers — prev, curr and next — and turns every next pointer around.

How it works

Complexity

Code (Python)

def reverse(head):
    prev, curr = None, head
    while curr:
        nxt = curr.next      # save the rest
        curr.next = prev     # turn the arrow
        prev, curr = curr, nxt
    return prev              # new head

Practise: Linked List problems