Skip to content

Linked list insertion and deletion

Insert a node after a given node and delete a node from a singly linked list by relinking next pointers, handling the head and the empty list, and say which of these operations cost O(1) and which O(n).

It opens on

To put 6 between 8 and 5 without moving any node, how many of the list's three next fields must change: none, one or all three?

Why it matters

Every linked structure, from a browser's history to an operating system's list of waiting jobs, is changed by rewriting a few next pointers, and nearly every bug in such code is a pointer rewritten in the wrong order or a head or empty list forgotten.

Good to know first

  • Singly linked lists: a Node holds data and next, the list is its head, and the last next is None
  • Classes, objects and __init__: Node(x) creates an object whose fields are set in __init__
  • Big-O, Big-Omega and Big-Theta: O(1) means a cost that does not grow with n and O(n) one that grows in proportion to n

Step by step

  1. Insert after a node

    Insert a new node after a given node with two assignments, in the right order, and give the cost as O(1).

    Call it with the node 8 and x = 6. Line 2 makes a node holding 6 whose next is None. Line 3 copies node.next into new.next, so the new node now points at 5.

    A common mix-up

    Inserting in the middle of a linked list shifts the later nodes along, as insert does in a Python list.

    In fact: No node moves; one new node is made and one old next is rewritten, so the cost does not depend on the length.

  2. Delete after a node

    Delete the node after a given node by rewriting one next, and explain why a node cannot be deleted without the node before it.

    Call it with the node 8. The right side is the node 6, then its next, the node 5, so the next of the node 8 becomes the node 5.

    A common mix-up

    To delete a node you change the node itself, such as setting it to None.

    In fact: Setting a variable to None does not unlink anything; the arrow into the node, held by the node before it, must be rewritten.

  3. Delete a value, the head and the empty list

    Write delete(head, x) for the head, the middle, a missing value and an empty list, return the new head, and give its best and worst costs.

    On 3 → 8 → 5 with x = 3 the function returns the node 8, and the list is 8 → 5. With x = 8 and a list that has just that node, it returns head.next, which is None, and the list is empty again.

    A common mix-up

    Deleting a value from a linked list costs O(1) because unlinking is two pointer moves.

    In fact: The unlinking is O(1), but finding the node before the value walks the list, so the whole delete is O(n) in the worst case.

  4. A tail pointer for append

    Keep a tail pointer so that append is O(1), keep it correct on the first append and on removing the last node, and say removing the last node is still O(n).

    For 3 → 8 → 5, the new node holds 7 with next None. The next of the node 5 becomes the new node, so the list is 3 → 8 → 5 → 7, and tail now names the node 7.

    A common mix-up

    With a tail pointer every operation at the end is O(1).

    In fact: Append is O(1), but remove at the end still walks from head to find the node before the tail, so it is O(n).

Try it

Starting from 2 → 4 → 9 → None, run insert_after on the node holding 9 with x = 1. What is the list, and what is the next of the new node?Answer

2 → 4 → 9 → 1 → None; the next of the new node is None

Hint: Line 3 copies the next of the node 9

In 7 → 2 → 9 → 4 → None, a is the node holding 2. After delete_after(a), what is the list?Answer

7 → 2 → 4 → None

Hint: a.next is the node 9, and a.next.next is the node 4

Which node does delete(head, 6) return as the new head, for 4 → 6 → 6 → 2 → None, and what is the list afterwards?Answer

The node 4 (head is unchanged); the list is 4 → 6 → 2 → None, since only the first 6 is deleted

Hint: The walk stops at the first match

A new LinkedList gets append(4), append(1), append(7), and then remove_last(). What are the list, tail, and the number of hops of remove_last?Answer

4 → 1, tail is the node 1, and 1 hop

Hint: Prev starts at the node 4 and stops when its next is the last node

Of the three old next fields, in 3, in 8 and in 5, how many did we change, and which?Answer

One: the next in the node 8, which used to point at 5 and now points at 6.

Linked list insertion and deletion

30 minutes every month. No card.