Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Linked List: Array to List

Linked Listseasy

A linked list node is represented as a plain object `{ val, next }`. Implement `arrayToList(arr)` that converts an array into a linked list and returns the head node. Example: `arrayToList([1,2,3])` → `{ val:1, next:{ val:2, next:{ val:3, next:null } } }`.

Sample tests

Input: arrayToList([1,2,3])
Output: {"val":1,"next":{"val":2,"next":{"val":3,"next":null}}}
Input: arrayToList([42])
Output: {"val":42,"next":null}

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Losing the head reference while advancing the tail.

Learning resources

  • MDN: Objects
Approach & explanation (try first)

Build nodes left to right, linking each to the next, and keep the head. O(n) time and space.

Loading...
⌘/Ctrl + Enter

Run your code to see results.