327. Reverse a Linked List Recursively

EasyLinked ListLinked ListRecursionReversal

Given the head of a singly linked list, reverse it using recursion and return the new head.

Recurse to the last node. On the way back, rewire each node's next pointer.

Input: Head of a singly linked list.

Output: Head of the reversed linked list.

Examples

Example 1
Input: [1,2,3,4,5]
Output: [5,4,3,2,1]
Explanation: Recurse to node(5) (base case). On return: 5.next→4→NULL rewired to 5→4→3→2→1.
Example 2
Input: [1,2]
Output: [2,1]
Explanation: Recurse to 2 (base). 2.next=1, 1.next=NULL. Return 2.
Example 3
Input: [7]
Output: [7]
Explanation: Base case (one node). Return 7.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →