← Course home

Linear structures · Lesson 12 of 52

Special linked-list cases

Read the preconditions behind duplicate removal, node deletion, digit lists, and circular splits.

Full lessonAbout 10 min

You will learn to state specialized linked-list preconditions and choose boundary tests for unusual input shapes.

Recommended first: Singly linked list.

On this page 6 sections

The idea

nth_from_end with a gap of 2 on 1, 2, 3, 4 lands on 3.

Some linked-list routines are valid only under a contract that is easy to miss. Values may be sorted. Digits may be stored least-significant digit first. A circular list may have a distinguished head. The function may receive one node and not the list head. nth_from_end makes its contract explicit. n is 1-based, and 1 means the tail. On 1, 2, 3, 4, nth_from_end with n equal to 2 returns the node holding 3. The leader walks n steps first, and then both pointers walk together. When the leader falls off, the trailer is the requested node. Removing adjacent duplicates in a sorted list needs only a neighbor comparison. The sorted precondition is what makes that enough. An unsorted list needs a set of seen values, which changes the memory. Deleting a node without the list head can copy the successor and bypass it. That trick cannot remove the tail, because there is no successor to copy. Adding numbers stored least-significant digit first can walk both heads and carry one digit.

A worked trace

StepStateWhat changed
gapstep = 1The leader has taken 1 steps. It sits on 2.
gapstep = 2The leader has taken 2 steps. It sits on 3.
walkstep = 2Both pointers move. The trailer is on 2.
walkstep = 3Both pointers move. The trailer is on 3.

Why it is correct

The gap stays n because both pointers move one step after the leader's head start. The trailer therefore sits n nodes behind the leader for the rest of the walk. No length subtraction is required. The sorted-duplicate claim fails as soon as an inversion exists, because a later duplicate is not beside its earlier copy. The headless delete fails on the tail for a structural reason, not an off-by-one. There is no next node whose value can be copied back. A circular split is wrong if either result is linear or if the two cycles share a node. Name the input shape, the output shape, and whether existing nodes are mutated.

A common mistake

The headless delete trick cannot remove the tail, because the tail has no successor.

What it costs

The leader takes n steps and then both pointers take the remaining steps. The total is linear in the length. Extra memory for the gap is two references. Counting the length and then walking length minus n is the same linear bound. The gap removes the subtraction, not the scan. A set of seen values for unsorted duplicates is linear extra memory. Copying a successor is constant time only when the successor exists. Test the empty list, one node, the last node, a duplicate run, and the smallest valid cycle or digit list. The gap walk is Θ(n) time and O(1) extra memory.

Run it

python
from bitsandbytes.linked_list import LinkedListfrom bitsandbytes.linked_lists.nth_from_end import nth_from_endchain = LinkedList()for item in (1, 2, 3, 4):    chain.append(item)print(nth_from_end(chain, 2).data)# prints: 3 

Practice and next step

Open Doubly linked-list operations next. A doubly linked node stores both neighbors, so a known node can be unlinked in place.

Implementation and tests Optional reference

Library code 5

  • bitsandbytes.linked_lists.duplicates
  • bitsandbytes.linked_lists.delete_node
  • bitsandbytes.linked_lists.add_numbers
  • bitsandbytes.linked_lists.split_circular

Use these references to compare the explanation with repository code and tests. A test link means it imports a lesson symbol; it does not by itself claim complete behavior coverage.

The interactive trace is loading. The written explanation below remains available.

Practice feedback is loading. The lesson explanation remains available above.