Linked lists
JavaScript · Unit 23: Data structures
- Full plan
- JavaScript
- 10 exercises
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. Predict the output
What does this code print?
2. Predict the output
What does this code print?
3. Complete the code
Fill in the blank so the walk moves on and ends.
4. Find the bug
It should print the three values of the list: 1, 2 and 3. Which line has the error?
5. Predict the output
How many hops does it take to reach the 3?
6. Complete the code
Fill in the blank to put the 0 at the front of the list.
7. Put the lines in order
Put
total(c)in order. It walks the linked list fromcand returns the sum of its values.8. Multiple choice
What does a linked list win over an array?
9. Find the case that fails
howMany(n)builds a linked list ofnnodes and returns how many it has. Ifnis 0, the list isnulland it should give 0. Which call breaks it?10. Predict the output
The three nodes were chained and one line changed an arrow. What does it print?
It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.