Facebook PixelReverse Linked List — Coding Practice
Reverse Linked ListEasy

Reverse Linked List

Easy 22.1k76% acceptance
Linked ListRecursion

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

The list is given to you as a ListNode ({ val, next }); return the head of the reversed list.

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: []
The empty list reverses to the empty list.
Constraints
  • The number of nodes is in the range [0, 5000].
  • -5000 ≤ Node.val ≤ 5000
Asked atAmazonMicrosoftAppleMeta
JavaScript
Loading editor…
Case 1
[1,2,3,4,5]
expected: [5,4,3,2,1]
Case 2
[1,2]
expected: [2,1]
Case 3
[]
expected: []
Case 4
[7]
expected: [7]
Case 5
[-3,0,3,9]
expected: [9,3,0,-3]