Reverse Linked List

EasyArrayMathSortingLinked ListGraph

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:

Each node's next pointer is redirected to point to its predecessor. Starting from 1->2->3->4->5, reversing each link to get 5->4->3->2->1.

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

With just two nodes (1->2), reversing the single link so that 2 points to 1, and 1 points to null, resulting in 2->1.

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

An empty list has no nodes to reverse. The head is null, so simply return null (represented as an empty array).

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!