DSA Visualizer · 25 visualizationsReverse Linked List
Arrays & Hashing
Two SumValid AnagramContains DuplicateGroup AnagramsTop K Frequent ElementsProduct of Array Except SelfLongest Consecutive SequenceStack
Valid ParenthesesBinary Search
Binary SearchTrees & BFS
Binary Tree Level OrderGraphs
Number of IslandsTrees
Invert Binary TreeEasyLinked 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 prevNext upMaximum Subarray
Two Sum is free. Enroll to unlock every visualization and save where you stopped.Enroll free
Watch the video walkthrough