Skip to content
6
0
:
0
0

Reverse Linked List

Easy

Linked List

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

Implement the solution iteratively.

Example 1

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

Reversing the linked direction changes the node order from left-to-right to right-to-left.

Example 2

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

After reversal, node 2 points to node 1, so the array form becomes [2,1].

Constraints

  • The number of nodes in the list is the range [0, 5000].
  • -5000 <= Node.val <= 5000
solution.js
Loading...

Test Cases (2)

Test Case 1
Input:
[1,2,3,4,5]
Expected Output:
[5,4,3,2,1]
Test Case 2
Input:
[1,2]
Expected Output:
[2,1]