All lessons

3. Linked Lists

Insert and delete

0 of 5 activities0%

Reading 1

Pointer rewiring

Open

Insert at head: newNode->next = head; head = newNode;

Insert after a node: newNode->next = cur->next; cur->next = newNode;

Delete head: Node* t=head; head=head->next; delete t;

Delete after prev: Node* t=prev->next; prev->next=t->next; delete t;

Never lose the only pointer to a node you still need.

Check 2

Insert front cost

Open

Inserting at the head of a singly linked list is

Fill in 3

Remove

Open

Freeing a node’s memory uses the keyword

Try it 4

Push front

Open

Insert 0 at front.

main.cpp
Loading editor…
Output will appear here.

Assignment 5

Delete head value

Open

Build list from n ints. Delete the head node if the list is non-empty, then print remaining values separated by spaces (or print empty line if none).

main.cpp
Loading editor…