Reverse a linked list in place
A singly linked list stores each value with a pointer to the next node. Reversing it means turning every arrow around. Three names track the work: the previous node, the current one and the next one you are about to lose.
class Node:
def __init__(self, value, next=None):
self.value, self.next = value, next
head = Node(1, Node(2, Node(3)))Think before coding
Save current.next before overwriting it, point current back at prev, then step both forward. When current becomes None, prev is the new head. No new nodes are created, so memory stays constant. Work through the example by hand before changing the starter. Decide what should happen for an empty input, a single value and a repeated value where those cases apply. The checks use several inputs so a hard-coded answer cannot stand in for the rule.
Your task
Node(value, next) is provided. Write reverse(head) that reverses the list in place and returns the new head (None for an empty list).
The explanation opens here once every check passes.