You get the head of a singly linked list: each node holds a value and a link to the next node, and the last node links to nothing. Reverse the list and return its new head.
Example: 7 → 3 → 9 → 4 becomes 4 → 9 → 3 → 7, and the function returns the node holding 4.
Try it yourself first: LeetCode 206 ↗. Then step through the solution below.
Copy the values into an array, then walk the list again and write them back in reverse order. That is O(n) time but O(n) extra space for the array, and it changes values rather than links. Building a second, reversed list node by node has the same O(n) space cost.
A node only knows the node after it, so the list can be reversed by turning each link around: a node should link to the node before it instead. The catch is that turning a link loses the way forward.
So walk with three variables: prev, the part already reversed; curr, the node being turned; and nxt, saved before the turn so the walk can go on. Each node is turned once.
The player runs the code on the example. Press → for each step: the lines it runs light up in the code.
On a phone, Full screen keeps the picture and the code on one screen.
class Node: def __init__(self, val, next=None): self.val, self.next = val, next def reverse(head): prev = None curr = head while curr: nxt = curr.next curr.next = prev prev = curr curr = nxt return prev
Now write it yourself on LeetCode 206 ↗, or go on: Merge Two Sorted Lists →
prev refers to no node yet (∅), and curr to the head, 7.nxt: once 7's link turns, nxt is the only way to reach 3.prev, which is no node (∅): 7 will be the end of the reversed list.prev to 7 and curr to 3.nxt.prev, 7.prev to 3 and curr to 9.nxt.prev, 3.prev to 9 and curr to 4.nxt refers to no node (∅).prev, 9.prev to 4, and curr past the end to no node: the loop ends.prev: 4, the head of the reversed list 4 → 9 → 3 → 7.Note
In Python the loop body is often one line: curr.next, prev, curr = prev, curr, curr.next. The right side is worked out first, but the names on the left are assigned one by one, from left to right. Put curr first, and the link is then turned on the next node instead of this one: the list breaks.
| Approach | Time | Space |
|---|---|---|
| Brute force: copy the values into an array | O(n) | O(n) |
| Recursion | O(n) | O(n) |
| Turn the links in one pass this page | O(n) | O(1) |
curr starts as None, the loop never runs, and the function returns None.None, where it already pointed, and the same node comes back as the head.1 → 2: two turns give 2 → 1.The blocks below are steps of Reverse Linked List in words, and one of them does not belong. Put the right ones in order and check: your algorithm runs on the example, 7 → 3 → 9 → 4, so a mistake shows in its answer. Once it is right, the code appears beside your blocks.
Click the blocks in the order they run, or drag them into the lines.Tap the blocks in the order they run. One of them doesn't belong. Each block keeps its indent: it runs inside the nearest line above it that ends in a colon.
Your algorithm
Blocks
Now write it yourself on LeetCode 206 ↗, or go on: Merge Two Sorted Lists →
class Node: def __init__(self, val, next=None): self.val, self.next = val, next def reverse(head): prev = None curr = head while curr: nxt = curr.next curr.next = prev prev = curr curr = nxt return prevCan you do it recursively? Yes: reverse everything after the head first, then make the head's next node link back to the head and set the head's link to None. Leave out that last part, and the first two nodes link to each other: a loop, not a list. It is also O(n) time, but each node waits on the call stack, so it takes O(n) space, and a very long list can exceed the recursion limit.
LeetCode is a trademark of LeetCode LLC. VisiGrab is not affiliated with LeetCode or with Blind; the problems are told in our own words, with our own examples.