πŸ“˜ CodingMarble Learn

Linked Lists

A linked list stores items in nodes. Each node holds data and a pointer (the address of the next node). A variable called head points to the first node; the last node points to NULL. Nodes can sit anywhere in memory, so adding or removing at the front takes only a pointer change, but finding the k-th item means walking from the head.

🎬 Step-by-step story

  1. An array keeps its items side by side in one block of memory, like seats in a row.
  2. A linked list keeps each item in a node. A node has two parts: the data (yellow) and a pointer to the next node (blue). Nodes can be anywhere.
  3. head points to the first node. To read the list we follow the arrows one node at a time until we reach NULL (the end).
  4. To insert at the front: make the new node point to the old first node, then move head to the new node. Just two pointer changes.
  5. To delete a node: make the node before it point to the node after it. The middle node is skipped and its memory is freed.
  6. Free play: insert, delete and search. Watch the step counter: searching means walking from head.

Tip: drag the 3D scene to turn it. Use two fingers to zoom.

πŸ€” Common doubts, cleared

Why not just always use an array?

Arrays are great for jumping to item k, but inserting or deleting near the front shifts many items. A list just changes arrows.

What exactly is stored in the blue part?

A memory address: the location of the next node. In Python it is a reference to the next object.

Why can't I jump straight to the 3rd node?

Only head is known. The 3rd node's address is stored inside the 2nd node, so you must walk.

Does the order of the two pointer changes in insert matter?

Yes. Link the new node to the old first node before moving head, or you lose the list.

What happens to the deleted node?

Nothing points to it any more. In C you free() it; in Python or Java the garbage collector removes it.

What is a linked list?

A data structure is a way to organise data in memory. A linked list is a chain of nodes. Each node has:

A variable head holds the address of the first node. The last node's next is NULL (None in Python), meaning "no more nodes". We draw nodes as boxes and pointers as arrows: 12 β†’ 7 β†’ 25 β†’ 3 β†’ NULL.

A linked list is recursive: a list is either empty, or one node followed by a smaller list.

Static vs dynamic memory

An array is static: you reserve a fixed block of side-by-side cells. Growing it means copying everything to a bigger block.

A linked list is dynamic: each node is created only when needed (malloc/new in C/C++, a new object in Python or Java) and freed when removed. Nodes do not need to be next to each other, because each one knows where the next lives.

You can also build a list statically inside an array: store data[] and next[] arrays, where next[i] is the index of the next item (βˆ’1 for the end). This is common in exam questions and in languages without pointers.

Operations: traverse, insert, delete, search, length

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

def push_front(head, x):
    n = Node(x); n.next = head
    return n   # new head

Linked list vs array, and its family

ArrayLinked list
Get k-th itemO(1) jumpO(k) walk
Insert/delete at frontO(n) shiftingO(1)
Sizefixed or re-copiedgrows node by node
Extra memorynoneone pointer per node

Doubly linked list: each node also has a prev pointer, so you can walk backwards. Circular list: the last node points back to the first. A stack is a list where you only push/pop at the head; a queue adds at the tail and removes at the head.

Try it

Write 5 numbers on 5 slips of paper. On the back of each slip write where the next slip is hidden in the room. Give a friend only the first slip (the head). Time how long it takes to reach the last one. Now insert a new slip at the front: what did you have to change?

Key formulas and definitions

Worked examples

1. List: head β†’ 4 β†’ 9 β†’ 2 β†’ NULL. Insert 7 at the front. Write the list.

new(7).next = head (node 4); head = new. List: 7 β†’ 4 β†’ 9 β†’ 2 β†’ NULL.

2. In 4 β†’ 9 β†’ 2, delete 9.

p = node 4. p.next = p.next.next, so 4 now points to 2. List: 4 β†’ 2 β†’ NULL.

3. How many nodes are visited to search for 2 in 7 β†’ 4 β†’ 9 β†’ 2?

Start at head: 7 (1), 4 (2), 9 (3), 2 (4). 4 nodes.

4. A static list: data = [D, A, C, B], next = [βˆ’1, 3, 0, 2], head = 1. Read the list.

Index 1 = A, next 3 = B, next 2 = C, next 0 = D, next βˆ’1 = end. List: A β†’ B β†’ C β†’ D.

5. Why is 'p.next = new; new.next = p.next' wrong?

After the first line p.next is already new, so new.next becomes new itself: a loop, and the rest of the list is lost. Set new.next first.

Common mistakes

Practice quiz

1. A node in a singly linked list has:
2. The last node points to:
3. Time to insert at the front of a linked list:
4. Which is faster in an array than in a linked list?
5. To delete the node after p we write:

Practice: answer these yourself

Type or choose your answer, then press Check. Use a hint if you are stuck; the full solution appears after you answer.

Frequently asked questions

What is a linked list in simple words?

A chain of boxes where each box holds a value and the address of the next box. You start at head and follow the addresses.

What is the difference between an array and a linked list?

An array stores items side by side and lets you jump to any index fast. A linked list stores items anywhere and links them, so inserting and deleting is cheap but access needs a walk.

What are the types of linked list?

Singly linked, doubly linked (next and prev) and circular (last node points back to the first).

Where this is taught

RomaniaClasa a XI-aDynamic data structures
RomaniaClasa a XI-aData structures
Germany (Bavaria)Jahrgangsstufe 12Lists

Learn first

Learn next

Related lessons

All Computer Science lessons