Practice

Reverse Linked List

Module 7 · Linked Lists

Problem

Given the head of a singly linked list, reverse it in place and return the new head.

Examples

Example 1

Inputhead = [1,2,3,4,5]Output[5,4,3,2,1]

Example 2

Inputhead = [1]Output[1]

Example 3

Inputhead = []Output[]

Constraints

0 ≤ length ≤ 5000 · follow-up: solve it iteratively AND recursively.

Attempt it first

The surgery lesson gave you the three-pointer walk and its invariant. Close the lesson, take paper, draw four nodes, and execute the dance by hand before you code it. This is THE canonical pointer exercise — worth doing honestly enough that you can rewrite it cold in an interview, because sub-range reversal shows up inside harder problems constantly.