All lessonsOpen Open Open Open Open
3. Linked Lists
Singly linked lists
0 of 5 activities0%
Reading 1
Chain of nodes
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
Getting the k-th element in a singly linked list is typically
Fill in 3
Start
Try it 4
Build three nodes
Print the list.
main.cpp
Loading editor…
Output will appear here.
Assignment 5
Length of list
Build a list from n integers (n then values). Print the number of nodes.
main.cpp
Loading editor…