BackmediumLinked Lists Accenture Flipkart

Collect Linked List Node Values Recursively Solution

Problem Statement

You are given the `head` of a singly linked list. Each node in the linked list has an integer `val` and a pointer `next` to the next node. Your task is to implement a function that recursively traverses the linked list from the `head` and collects all node values into a new list (e.g., an array or ArrayList) in the order they appear. The function should return this collected list.

Example 1
Input
Linked List: 1 -> 2 -> 3 -> 4 -> NULL
Output
[1, 2, 3, 4]

Explanation: The function recursively visits each node, collecting its value into a list in traversal order.

Example 2
Input
Linked List: 7 -> NULL
Output
[7]

Explanation: The list contains only one node, so its value is collected.

Constraints

  • 0 <= Number of nodes in the list <= 1000
  • -1000 <= Node.val <= 1000
  • Node values are not necessarily unique.
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free