313. Print Linked List in Reverse (Recursive)

EasyLinked ListLinked ListRecursionReversal

Given the head of a singly linked list, return an array of node values in reverse order using recursion (do NOT reverse the list itself).

Recurse to the end first, then collect values on the way back.

Input: Head of a singly linked list.

Output: Array of node values from tail to head.

Examples

Example 1
Input: [1,2,3,4,5]
Output: [5,4,3,2,1]
Explanation: Recurse to end: base case at NULL. Return [5], then prepend 4,3,2,1.
Example 2
Input: [10,20,30]
Output: [30,20,10]
Explanation: Recurse: reach 30, then 20, then 10. Collect: [30,20,10].
Example 3
Input: [7]
Output: [7]
Explanation: Single node. Base case: return [7].

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →