Asked at

Reverse Linked List

Medium
Verified
Linked ListRecursion~20 min

Given the values of a singly linked list as an array head (head first), reverse the list and return the new order.

Aim for an O(n)-time solution. Consider both the iterative and the recursive approach.

Examples

in[1, 2, 3, 4, 5]
out[5, 4, 3, 2, 1]

The list is returned in reverse order.

in[]
out[]

An empty list reverses to itself.

Constraints

  • The number of nodes is in the range [0, 5000].
  • -5000 ≤ Node.val ≤ 5000
  • Target: O(n) time, in place.

Get help

🔑

Sign in to solve

Sign in to write, run, and submit your solution — and to pick up where your iOS flow left off.