Technical skill practice
Python Linked List: Build, Traverse and Remove a Node
A singly linked list connects nodes through next references. Python’s built-in list is a different container; it does not expose linked nodes. The implementation here is an educational model that makes pointer-style updates visible, not a recommendation to replace ordinary lists in everyday Python code.
Start with a small, explicit task
Build three fictional stage nodes, remove the middle stage, then remove the head. Return the new head after removal so the caller can retain a changed entry point. Also check a missing label and an empty structure. Each label is unique in this fixture; the removal rule searches for the first equal value.
Run the complete example
This original example was checked with Python 3.12.14. Save it as python_linked_list.py and run it with Python 3.12. It uses only the standard library. The assertions check the stated results.
Expected output
['send']
middle, head, missing and empty checks passed
from dataclasses import dataclass
@dataclass
class Node:
label: str
next: "Node | None" = None
def values(head):
result = []
current = head
while current is not None:
result.append(current.label)
current = current.next
return result
def remove_first(head, target):
previous = None
current = head
while current is not None:
if current.label == target:
if previous is None:
return current.next, True
previous.next = current.next
return head, True
previous, current = current, current.next
return head, False
head = Node("draft", Node("review", Node("send")))
assert values(head) == ["draft", "review", "send"]
head, removed = remove_first(head, "review")
assert removed and values(head) == ["draft", "send"]
head, removed = remove_first(head, "draft")
assert removed and values(head) == ["send"]
head, removed = remove_first(head, "missing")
assert not removed and values(head) == ["send"]
assert remove_first(None, "missing") == (None, False)
print(values(head))
print("middle, head, missing and empty checks passed")
Explain the result
For middle removal, previous points to draft and current points to review. Updating previous.next to current.next bypasses review without rebuilding the other nodes. For head removal there is no previous node, so the function returns the next node as the new head. The caller assigns that result back to head.
Traversal repeatedly follows next until None. It assumes an acyclic structure, as the constructed fixture is; a cycle would require a separate detection or ownership rule. Searching for a missing value visits all remaining nodes and reports False without changing the entry point. These assertions verify the small stated structure, not a full collection implementation.
Avoid these mistakes
Do not ignore the returned head when removing the first node. Do not claim constant-time arbitrary lookup: reaching a label requires traversal. The example omits indexing, concurrency, cycle protection and a production container API. Use a standard built-in container or deque when it already fits the actual task.
Questions, answered
Is Python list a linked list?
It is a different built-in sequence. This tutorial explicitly constructs nodes and next references instead.
Why return the head after removal?
Removing the first node changes the entry point; the caller must retain the new head.
Sources and examples
ResumizeAI team. Examples are fictional teaching material, not customer results. Replace sample details with your own facts.
Your experience. A complete resume.
Build in Resumize with ATS-friendly templates, guided editing and a live preview. Start free, then upgrade when you need more.
Free account required. Free-plan PDF exports include a watermark.