 All Problems
Reverse Linked List
easy
linked list
recursion
amazon
facebook
microsoft
apple

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

Example 1:

Input:  1 2 3 4 5
Output: 5 4 3 2 1

Example 2:

Input:  1 2
Output: 2 1

Example 3:

Input:  (empty)
Output: (empty)

Constraints:

  • The number of nodes in the list is in the range [0, 5000]
  • -5000 ≤ Node.val ≤ 5000

Input format: A single line of space-separated integers representing the linked list nodes in order. Empty line for empty list.

Output format: A single line of space-separated integers representing the reversed linked list.

Run to check your code against the sample cases, or submit to run every case