LOGO100 logo

Linked List

You will receive an array with nodes of a linked list and the ID of the top node as seen below.

Submit as solution an array of the values in the order that they appear in the linked list.

Example puzzle

{
    "linkedList": [
        { "id": "b", "value": 2, "next": "c" },
        { "id": "c", "value": 3, "next": null },
        { "id": "a", "value": 1, "next": "b" }
    ],
    "top": "a"
}

Example solution

[ 1, 2, 3 ]

Actual data used in the competition

Explanation

This challenge was easiest to solve using recursion:

Code solutions

There are many ways to solve this. This JavaScript solution does a good job of using find():

const topNode = linkedList.find(node => node.id === top);
const tree = [];
const traverse = node => {
    if (!node) return;
    tree.push(node.value);
    traverse(linkedList.find(n => n.id === node.next));
    return tree;
};

traverse(topNode);

Other submissions:

Back to all puzzles