Tutelage

Lessons › coding and computer science · taught by Leo

Linked lists explained: nodes, links, and traversal

Learn how linked lists store data in nodes, use next pointers, and support safe traversal, insertion, and deletion.

Learn this with Leo, freeA voiced lesson with a live whiteboard, quizzes and a tutor you can call, at tutelage.app

Why linked lists are chained nodes

Think of a linked list like a chain of little boxes. Each box holds data and a link to the next box.

The list starts at the first node. If you know that one, you can follow the links to the rest.

The last node points to null, which means there is no next node.

What one node stores

Each node has two parts: the value, and a reference to the next node.

That is why a linked list does not need all its items stored next to each other in memory.

The links matter more than the physical location.

Predict the next pointer idea

If a node is the last node in a linked list, what does its next pointer store?

  1. A reference to the next node
  2. null
  3. The previous node
Show answer

null: Yes. The last node does not point to another node, so next is null.

Follow links to read the list

Here is a tiny list: A, then B, then C. The head points to A.

To read it, you do not jump around. You start at head and follow next each time.

That is why linked lists are good for growing one node at a time.

Quick check on traversal

You are at node B. What should you do next to move forward in the list?

  1. Go to B's next pointer
  2. Go back to head
  3. Stop and reread A
Show answer

Go to B's next pointer: Correct. From B, you follow B's next pointer to the next node.

Why insertion can be cheap

A big advantage is insertion. You can often change just a few links instead of shifting many items.

That is faster than an array when you insert near the front or in the middle.

The tradeoff is that you must walk through nodes to find a position.

Insert one node between two

To insert a new node, first let the new node point to the old next node.

Then let the previous node point to the new node.

The chain is repaired with just a couple of link changes.

Predict the insertion move

To insert a node between X and Y, after you have saved the old next node, which link should be changed first?

  1. X.next should point to the new node
  2. Y.next should point to X
  3. The new node should point to null
Show answer

X.next should point to the new node: Yes. After you have saved the old next node, connect X to the new node, then connect the new node to Y.

Common mistake: losing the chain

Watch out for one classic bug: if you overwrite a link too soon, you lose the rest of the list.

So you save the old next node first. Then you can safely change pointers.

That small habit prevents broken chains.

Your turn: safe deletion

You want to delete node B from A → B → C. What is the safe first step?

  1. Set A.next to C using B.next
  2. Set B.next to A
  3. Delete A first
Show answer

Set A.next to C using B.next: Good. Save or use B.next, then make A.next skip B and point to C.

Linked list essentials to remember

Here is the checklist: start at head.

Follow next to move through the list.

Use null to mark the end, and relink carefully when you insert or delete.

Learn this with Leo, freeA voiced lesson with a live whiteboard, quizzes and a tutor you can call, at tutelage.app