Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Delete the first occurrence of a value

Remove the first node with a given value and return the updated list head.

Python practice10 minLinked ListsBeginnerLast updated March 27, 2026

Problem statement

Given the head of a singly linked list and an integer target value, delete the first node in the list whose value equals the target. Return the head of the modified list. If the target is not found, return the original head. If the deleted node is the head, return the new head. If the list becomes empty, return None. You may create a dummy node to simplify deletion logic.

Task

Implement deletion of the first node with the target value from a singly linked list and return the (possibly new) head.

Examples

Delete middle node

Input

head = build_linked_list([1, 2, 3, 2]) linked_list_to_list(delete_first(head, 2))

Output

[1, 3, 2]

The first 2 (index 1) is removed; remaining nodes are 1 -> 3 -> 2.

Input format

Two arguments: head (ListNode or None), value (int). Return value should be a ListNode or None (but tests may convert to a Python list using linked_list_to_list).

Output format

Return the new head (ListNode or None). Tests in this lesson convert to Python list for comparison.

Constraints

- Node values and target are integers. - Perform only one pass until the first matching node is removed. - Use O(1) additional space.

Samples

Sample 1

Input

build_linked_list([1,2,3]), 1

Output

[2, 3]

Deleting the head updates the head to the next node.