algoritmauzmanı
TREN
3Linked Lists
Lesson 3.0

Introduction to linked lists

keep their elements side by side in memory and reach any element instantly by its . But inserting in the middle or growing the array is costly. A solves both problems with a different layout. This lesson covers the idea behind linked lists, their three kinds and how they compare with arrays.

What you will learn
  • Which operations are hard for an array and how a linked list handles them
  • The idea of nodes and links, and how singly, doubly and circular lists differ
  • Which operations suit an array and which suit a linked list
7 min

1Two limits of the array

An keeps its elements in . The address of the fourth element is computed from the address of the first, so every element is reached in one step.

120x10070x10430x10890x10c
An array of four elements. The addresses follow each other 4 bytes apart: the elements sit side by side in memory.

The same layout brings two problems. First, to insert at the front or in the middle, every element behind that spot moves one place; in an array of n elements that is work. You can see this shifting in Inserting into an array.

Second, the size of the array is fixed up front. When it fills up, a bigger block is allocated and every element is copied over. Dynamic arrays do this for you, but the copying has a cost.

A linked list does not put its elements side by side. Each element lives in its own , and next to the value the node holds the address of the next node. That address is called a link; a link is a . points to the start of the list, and the last node's link is .

NULL12?0x1a407?0x2c103?0x1b20head
A linked list of three nodes. The addresses are scattered; the order comes from the links, not from the place in memory.

Inserting a new element moves no elements: the new node is allocated anywhere in memory and a few links change. The list also grows one node at a time; there is no size to fix in advance.

There is a price: the nodes are not side by side, so you cannot jump to the 3rd element. You start at the front and follow the links one by one.

3Three kinds of linked list

Singly linked list

Each node knows only the node after it. You can only move forward. It is the simplest kind, and the first part of this section is about it.

NULL12?0x1a407?0x2c103?0x1b20head
Singly linked: one link per node, the last link is NULL.

Doubly linked list

Each node knows both the next node () and the previous one (). You can move in both directions, and deleting a node does not require searching for the one before it. In return every node carries one extra pointer.

NULLNULL12?0x1a407?0x2c103?0x1b20head
Doubly linked: next links going right and prev links going left. The first node's prev and the last node's next are NULL.

Circular linked list

The last node points to the first node instead of NULL, so the list forms a ring. Work that goes round in turns, such as passing the turn between players in a game, fits this shape well. When traversing you must know where to stop, because NULL never comes.

12?0x1a407?0x2c103?0x1b20head
Circular: the last node's link goes back to the front, to 12.

4Array or linked list?

Both do the same job: they hold a sequence of elements. Which one fits depends on the operation you perform most often.

Operation or propertyArraySingly linked list
Access by index (k-th element)O(1)O(n): walk from the front
Insert at the frontO(n): every element shiftsO(1)
Insert at the endO(1) if there is roomO(n); O(1) with a tail pointer
SizeFixed up frontGrows as nodes are added
Extra memoryNoneOne pointer per node
Cache friendlinessGood: elements side by sidePoor: nodes scattered

The last row matters in practice. When the processor reads a value from memory, it also brings the values next to it into the . In an array the next element is usually already there; in a linked list the next node may be somewhere else in memory. So of two traversals with the same , the array one usually finishes faster.

In short: if you mostly reach elements by index, use an array; if you often insert and delete at the front or in the middle, a linked list fits better.

5What this section covers

Every operation lesson has a step-by-step drawing, code in C, Java and Python, and the complexity of the operation. The starting point is What is a singly linked list?.

6Try it

Why is inserting at the front of an array of 1000 elements costly?

Show the answer

The answer is B. In an array the elements must sit side by side; to make room at the front, all of them move. In a linked list only a few links change, see Inserting at the head.

In which operation is an array clearly faster than a linked list?

Show the answer

The answer is B. In an array the address of the k-th element is computed, which takes . In a linked list you walk k steps from the front to reach the k-th node.

Lessons you mark show up in the curriculum. This is kept on this device only.