36. Reverse Linked List

Easy · Linked List

Given a singly linked list, reverse it in-place and return the head of the reversed list.

A singly linked list is represented as an array where each element is a node value. For example, [1, 2, 3] represents a list where 1 points to 2, which points to 3, which points to null.

Your task is to reverse the order of nodes so that the last node becomes the head and each node points to its predecessor.

Examples

Example 1
Input: [1, 2, 3, 4, 5]
Output: [5, 4, 3, 2, 1]
Explanation: The list 1→2→3→4→5→null becomes 5→4→3→2→1→null.
Example 2
Input: [1, 2]
Output: [2, 1]
Explanation: The list 1→2→null becomes 2→1→null.

Constraints