A2Z Lesson

Log in
← Back to the A2Z sheet

Step 08 Linked-List — Fundamentals (Single LL)

Insertion in Linked List

Medium

Insertion is the mirror image of deletion: create a node, then wire it into the chain in the right order so nothing after the insertion point is lost. This lesson covers the four standard cases — at the head, at the tail, at position k, and before a given value — and the one ordering rule that prevents the most common linked-list bug.

The golden rule of insertion order

When splicing a new node between A and B, always point the new node at B first, and only then point A at the new node.

If you rewire A first (A.next = new_node) before saving B, the reference to B — and the entire rest of the list — is gone. Every insertion case below is just: find the splice point, then apply 'new node points forward first, predecessor points at new node second'.

Case 1: insert at head, Case 2: insert at tail

At the head: the new node's next is the old head, and the new node becomes head. O(1), and it works on an empty list with no special casing (old head is None, which is exactly the right next).

At the tail: if the list is empty, the new node is the head. Otherwise walk to the last node (temp.next is None) and attach the new node there. O(n) for the walk.

def insert_head(head, x): return Node(x, head) # new node -> old head def insert_tail(head, x): new_node = Node(x) if head is None: return new_node temp = head while temp.next is not None: temp = temp.next temp.next = new_node return head

Case 3: insert at position k

Positions are 1-indexed: inserting at position k means the new node becomes the k-th node, and the node previously at k shifts to k+1, so you splice after the (k-1)-th node. If k == 1, it is the head case. If k is beyond length+1, most problem statements treat it as invalid or as an append — read the statement.

Walk with a counter to position k-1, then apply the golden rule: new_node.next = temp.next; temp.next = new_node.

def insert_at_k(head, x, k): new_node = Node(x) if k == 1: new_node.next = head return new_node temp = head count = 1 while temp is not None and count < k - 1: temp = temp.next count += 1 if temp is None: # k out of range return head new_node.next = temp.next # 1) point forward first temp.next = new_node # 2) then link predecessor return head

Case 4: insert before a value X

Walk until you find the node whose next holds X — you must stop one node early, because the splice happens at the predecessor. If X is the head's value, it is the head case. If X is absent, the list is left unchanged.

All four insertion cases are O(n) worst case (except head insertion, O(1)) and O(1) extra space besides the new node itself.

def insert_before_value(head, x, val): # insert val just before the first node containing x if head is None: return None if head.data == x: return Node(val, head) temp = head while temp.next is not None and temp.next.data != x: temp = temp.next if temp.next is not None: temp.next = Node(val, temp.next) return head

Common mistakes

(1) Rewiring the predecessor before setting the new node's next — the golden-rule violation that loses the tail of the list. (2) Off-by-one on position k: remember you stop at k-1 because the new node takes position k. (3) Forgetting the empty-list and head-value special cases in the tail and before-value routines. (4) Not returning head from every insert function — inserting at the head changes head just like deleting it did.

Key points

  • Golden rule: set the new node's next first, then link the predecessor to it — never the other way round.
  • Insert at head is O(1); tail, position-k and before-value insertions walk first, so O(n).
  • Position k insertion splices after node k-1; k == 1 is the head case.
  • Before-value insertion must stop at the predecessor of the target node.
  • Every insert routine returns the (possibly new) head.

Suggested questions

Insertion at the head of Linked List ↗ Case 1 — the only O(1) insertion.
Insertion at the tail of Linked List ↗ Case 2 — walk to the last node, then attach.
Insertion at the Kth position of Linked List ↗ Case 3 — watch the stop-at-k-1 off-by-one.
Insertion before the value X in Linked List ↗ Case 4 — stop at the predecessor of the node holding X.

Source note: this lesson follows the structure of Striver's public A2Z DSA course (takeUforward). Each problem links to its original platform, and all problems and content belong to their respective owners — STUDY-BUDDY does not claim ownership of the sheet or its problems, and no permission beyond public linking is claimed.