Introduction to Linked Lists

So you have already used arrays. They are great for storing a list of things. But arrays have a problem. The size is fixed. And adding something in the middle is slow. A linked list fixes both of these. Let us see how, step by step, in plain words.

πŸ“ The Problem With Arrays

Think about an array. When you create it, you pick a size. Say you make room for 5 students. Now a 6th student joins. There is no space left. So you have to make a bigger array and copy everything over. That is slow and wasteful.

Here is the other pain. Say you want to add a student right in the middle of the list. In an array, every element after that spot has to move one place to the right. Insert near the front of a big array, and you move almost everything. That is a lot of work for one tiny insert.

So arrays give you two pains:

  • The size is fixed when you create it. Growing means copying everything into a bigger array.
  • Inserting or deleting in the middle is slow. Other elements must move out of the way.

A linked list is a data structure that solves both of these. It can grow as you go. And it can insert in the middle without moving anything.

πŸ—ΊοΈ A Treasure Hunt Analogy

Imagine a treasure hunt game. You start with one clue. That clue does not hold the treasure. It just tells you where the next clue is. You go there, find the next clue, and it points to the next spot. You keep following the chain until you reach the treasure.

Notice something. The clues are not sitting next to each other in one neat row. They can be anywhere in the park. Each clue only needs to know one thing. Where the next clue is.

A linked list works the same way. Each piece of data lives in its own little box. And each box holds a direction to the next box. You follow the chain from the first box to the last.

πŸ“¦ What Is a Node?

The little box in a linked list has a name. We call it a node. A node is a single unit. It holds two things together.

  • The data β€” the value you want to store, like a number or a name.
  • The next pointer β€” a link that tells you where the next node is.

That second part is the important one. The β€œnext” is a pointer. A pointer is just an address that says β€œthe next node lives here.” Follow it and you reach the next node.

The very first node has a special name. We call it the head. The head is your entry point. If you know the head, you can reach every other node. You follow the next pointers one by one.

The last node points to nothing. In most languages we write that as NULL or None. It means β€œthe chain ends here. There is no next node.”

πŸ”— How the Chain Looks

Let us draw a linked list that stores 10, then 20, then 30. Each box is a node. The arrow is the next pointer.

Head

10 | next

20 | next

30 | NULL

Read it like this:

  • The head points to the first node, which holds 10.
  • The node with 10 points to the node with 20.
  • The node with 20 points to the node with 30.
  • The node with 30 points to NULL. So we know it is the last one.

Notice the nodes do not need to sit side by side in memory. Each one just remembers the address of the next. That is why a linked list can grow freely. To add a new node, you find some free space, put your data there, and fix the pointers to include it. Nothing else has to move.

Tip

A linked list is just nodes connected by pointers. If you hold the head, you hold the whole list. Lose the head and you lose the list. There is no other way back to the start.

⚑ Why Inserting Is Cheap

Remember the array pain? Inserting in the middle meant moving everything. In a linked list, you do not move anything.

Say you want to put a new node with 15 between 10 and 20. You only change two links:

  • Make the new node’s next pointer point to 20.
  • Make the node with 10 point to the new node instead of 20.

That is it. The other nodes never move. So an insert in a linked list, once you are at the right spot, is just a couple of pointer changes. No copying. No shifting.

10

15

20

30

The trade is fair though. You get cheap inserts, but you give up something else. We will see that cost in the next section.

βš–οΈ Array vs Linked List

Both store a list of things. But they behave very differently. Here is a side-by-side look at the parts that matter most.

What you do Array Linked List
Read the element at a position Fast, jump straight to it β€” O(1) Slow, walk from the head β€” O(n)
Insert or delete in the middle Slow, move other elements β€” O(n) Cheap, fix a few pointers β€” O(1) at the spot
Size Fixed, grow by copying Grows freely, node by node
Memory Just the data, all together Data plus a pointer per node, spread out

So see the pattern. Arrays are great when you read a lot by position. Linked lists are great when you insert and delete a lot. Pick the one that matches what your program does most.

Note

A linked list cannot jump to β€œthe 5th element” directly. There is no index math like an array. You must start at the head and follow the chain five times. That is the price you pay for cheap inserts.

πŸ’» Defining a Node in Code

Before you can build a linked list, you define one node. A node holds the data and a link to the next node. Here is the smallest version in five languages. We make one node, fill in its data, and start it with no next node yet.

node.py
# A node holds data and a link to the next node
class Node:
def __init__(self, data):
self.data = data # the value we store
self.next = None # no next node yet
# Make one node
head = Node(10)
print("Data:", head.data)
print("Next node exists:", "yes" if head.next else "no")

The output of the above code will be:

Output
Data: 10
Next node exists: no

See how small a node is? Just data and a next link. Once you have this one building block, you can chain many nodes together. And then you have a full linked list.

⚠️ Common Mistakes Beginners Make

A few things trip up almost everyone at the start. Watch out for these.

  • Losing the head. If you move the head pointer forward and do not save the old start, you cannot reach the front of the list anymore. Always keep the head safe.
  • Forgetting the last node points to NULL. If the last node points somewhere random, your loop never ends. The chain must end at NULL or None.
  • Thinking you can index like an array. There is no list[3] jump in a linked list. You walk from the head every time.
  • Breaking links in the wrong order during an insert. Point the new node to the next node first. Then point the previous node to the new node. Do it the other way and you lose the rest of the list.

🧩 What You’ve Learned

βœ… Arrays have a fixed size and slow middle inserts. A linked list fixes both.

βœ… A linked list is made of nodes. Each node holds data plus a pointer to the next node.

βœ… The head is the first node and your entry point. The last node points to NULL or None.

βœ… Inserting in a linked list is cheap because you only change a few pointers. There is no shifting.

βœ… Reading by position is slower than an array. You must walk from the head.

Check Your Knowledge

Test what you learned. Pick an answer for each question, then click Check.

  1. 1

    What two things does a single node in a linked list hold?

    Why: Each node stores its data plus a next pointer that links to the following node.

  2. 2

    Why is inserting in the middle of a linked list cheaper than in an array?

    Why: An insert just rewires a couple of pointers, so the other nodes never move.

  3. 3

    What does the last node in a singly linked list point to?

    Why: The last node points to NULL or None, which marks the end of the chain.

  4. 4

    Compared to an array, reading the element at a given position in a linked list is:

    Why: A linked list has no index jump, so you follow the next pointers from the head β€” O(n).

πŸš€ What’s Next?

Share & Connect