linked-lists · Problem 1 of 1

Reverse Linked List

easy
Amazon logoAmazon
Google logoGoogle
Meta logoMeta
Microsoft logoMicrosoft
Apple logoApple

Given the head of a singly linked list represented as an array of values, reverse the list and return the reversed array of values.

reverseList([1, 2, 3, 4, 5]) -> [5, 4, 3, 2, 1]
reverseList([1, 2])          -> [2, 1]
reverseList([])              -> []

Your solution

Runs your code and animates it without grading anything. Change the input to see what it does on a case the tests do not cover.

Running is free — Submit is what records it. Or press ⌘↩

Tests

4 cases, 1 hidden
calltypeexpectedresult
reverseList([1,2,3,4,5])five elements[5,4,3,2,1]
reverseList([1,2])two elements[2,1]
reverseList([1])single element[1]
withheldhiddenwithheld

Hidden cases run too — their inputs aren't listed here, so aim for a general solution rather than one fitted to the cases above.

Complexity

target time
O(n)
target space
O(1) auxiliary pointer manipulation (or O(n) array space)

Iterating through n nodes reversing pointers in-place takes linear time with no additional heap allocations.

Hints

Stuck? Hints open one at a time, each giving a little more away.

3 hints left