All topics
44

A chain of boxes, each pointing to the next

Tier 1Locked

Where it runs

1. The problem

An array lets you read any item at once by its position number (its index). Inserting in the middle is costly, though: everything after the insertion point has to shift one cell along. A linked list works the other way round. You can't read an item by its number at once, but you can insert or cut out an item in one step, as long as you already stand next to that place.

An array stores its values in one solid block of memory, one after another. So reading position 5 is instant, while inserting at the front is costly: every later item has to shift. A linked list stores each value in a separate node. A node holds the value itself and a link to the next node, and so the nodes form a chain. Adding an item at the front is instant. Jumping to position 5 is impossible, though: you have to walk there along the chain.

Later lessons have harder tricks: reversing a list, finding a loop in it, merging two lists. For those you first need the simple part: build a chain, walk along it, and read values from it. That is what this lesson does.

This step is part of the paid track

The first 33 steps are open to everyone: lesson, exercise and tests. This one opens with full access. Start with a free account and the free steps.

Create a free accountSee what is included

On LeetCode

Problems that hand you the head of a linked list and ask you to walk it, to read every value, collect the values, or build a new list as you go.

We use necessary cookies to run the site (sign-in and language). If you accept, we also load Google Analytics to see which pages are used, and Google reCAPTCHA to keep spam off the contact and bug-report forms. Privacy policy