All lessons

3. Linked Lists

Singly linked lists

0 of 5 activities0%

Reading 1

Chain of nodes

Open

A singly linked list node holds a value and a pointer to the next node. The list is remembered by a head pointer.

Unlike arrays, inserts at the front are O(1), but random access is O(n) because you walk links.

Always handle empty lists (head == nullptr).

struct Node {
  int data;
  Node* next;
  Node(int v): data(v), next(nullptr) {}
};

Check 2

Access cost

Open

Getting the k-th element in a singly linked list is typically

Fill in 3

Start

Open

The pointer to the first node is usually called

Try it 4

Build three nodes

Open

Print the list.

main.cpp
Loading editor…
Output will appear here.

Assignment 5

Length of list

Open

Build a list from n integers (n then values). Print the number of nodes.

main.cpp
Loading editor…