311. Convert Linked List to Array

EasyLinked ListLinked ListArrayTraversalConversion

Given the head of a singly linked list, convert it to an array (list) and return the array containing all node values in the same order from head to tail.

Traverse the list once and collect all values.

Input: Head of a singly linked list.

Output: An array of integers representing node values from head to tail.

Examples

Example 1
Input: [1,2,3,4,5]
Output: [1,2,3,4,5]
Explanation: Traverse: 1→2→3→4→5→NULL. Collect [1,2,3,4,5].
Example 2
Input: [10,20,30]
Output: [10,20,30]
Explanation: Traverse: 10→20→30→NULL. Collect [10,20,30].
Example 3
Input: [7]
Output: [7]
Explanation: Single node. Collect [7].

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →