Skip to the content

Linked lists

JavaScript · Unit 23: Data structures

A linked list doesn't keep everything together the way an array does: it's loose nodes, and each node is an object with its value and an arrow to the next one.

The arrow is called next, and the last node points at null, which is the sign that it ends there.

const c = { v: "c", next: null };
const b = { v: "b", next: c };
const a = { v: "a", next: b };
console.log(a.v, a.next.v);

Prints

a b

The rest of the explanation is in the lesson, which is part of the full plan.

Exercises in this lesson

You do them in the app, which checks them on the spot and explains why.

  1. 1. Predict the output

    What does this code print?

  2. 2. Predict the output

    What does this code print?

  3. 3. Complete the code

    Fill in the blank so the walk moves on and ends.

  4. 4. Find the bug

    It should print the three values of the list: 1, 2 and 3. Which line has the error?

  5. 5. Predict the output

    How many hops does it take to reach the 3?

  6. 6. Complete the code

    Fill in the blank to put the 0 at the front of the list.

  7. 7. Put the lines in order

    Put total(c) in order. It walks the linked list from c and returns the sum of its values.

  8. 8. Multiple choice

    What does a linked list win over an array?

  9. 9. Find the case that fails

    howMany(n) builds a linked list of n nodes and returns how many it has. If n is 0, the list is null and it should give 0. Which call breaks it?

  10. 10. Predict the output

    The three nodes were chained and one line changed an arrow. What does it print?

Do this lesson

It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.

See all lessons →