AlgoLounge

Reverse Linked List

easy

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

Each node has a value and a pointer to the next node:

class ListNode {
  val: number
  next: ListNode | null
}

Test cases are written as plain arrays — [1,2,3,4,5] means the chain 1 → 2 → 3 → 4 → 5. Your function receives real ListNode objects, and the list you return is walked back into an array for comparison.

Examples

Example 1

Input:  head = [1,2,3,4,5]
Output: [5,4,3,2,1]

Example 2

Input:  head = [1,2]
Output: [2,1]

Example 3

Input:  head = []
Output: []

An empty list: your function receives null (Python: None).

Constraints

  • 0 <= number of nodes <= 5000
  • -5000 <= node.val <= 5000

Follow-up: the iterative version uses O(1) extra space. Can you also write it recursively — and what does that cost?

TypeScript
Loading editor…
List of test cases (5)
Case 1
head = [1,2,3,4,5]
Case 2
head = [1,2]
Case 3
head = []
Case 4
head = [1]
Case 5
head = [-3,0,7,7]