‹ DS&A interview · Socratic
DSA · Linked List · #150

Reverse Linked List

Module 42 · difficulty 2/5·30:00starts on first keystroke

Given the `head` of a singly linked list, reverse the list and return the new head. Each node has the shape `{ val, next }`, where `next` is the next node or `null`. Implement the function `reverseList(head)`; it returns the head of the reversed list (or `null` for an empty list).

Examples
  • head = [1,2,3,4,5] [5,4,3,2,1]Every pointer is flipped; the old tail becomes the new head.
  • head = [1,2] [2,1]
  • head = [] []Empty list reverses to itself.
Constraints
  • · The number of nodes is in the range [0, 5000].
  • · -5000 <= Node.val <= 5000
  • · Solve it both iteratively and recursively.
Session phases
A · Clarify
B · Approach
C · Complexity
D · Edges
E · Code
F · Tradeoff
G · Score
Phase A — Clarify
Ask questions about input bounds, types, and edge constraints.
Ask the coach clarifying questions about the problem.
When you've covered this phase, advance to the next.