Skip to interview prep
Apexed

13. Reverse a Linked List

EasyLinked lists

Reverse a singly linked list and return the new head.

A linked list is passed as ListNode objects with val and next. The tests write lists as arrays.

Example 1

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

Constraints

  • 0 ≤ nodes ≤ 5000

Tab indents · Ctrl/⌘ + Enter runs · Esc then Tab leaves the editor

Run checks these cases; the expected output comes from the reference solution. Submit judges all 3 hidden and sample tests.

Code runs only in your browser in an isolated sandbox. Progress and submissions are saved on this device.

Reverse a Linked List — coding interview practice — Apexed