Description

Given the head of a singly linked list, reverse the list and return the reversed list. The linked list nodes contain integer values, and you should reverse the order of all nodes, not just their values.

Examples

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

The linked list is reversed.

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

The two nodes are swapped.

Input:head = []
Output:[]
Explanation:

Empty list remains empty.

Constraints

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

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!