Remove every odd-valued node from a singly linked list

Quick Overview

Remove every node with an odd value from a singly linked list and return the new head, keeping the even values in their original order. It tests pointer updates when the head or a run of consecutive nodes must be removed, and correct parity checks for zero and negative values.

Remove every odd-valued node from a singly linked list

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given the `head` of a singly linked list, remove all nodes whose value is odd, and return the new head of the resulting linked list. A value is odd if it is not divisible by 2. Only nodes with even values (including zero and negative even numbers) should remain in the list, preserving their original order. ### Function Signature ```python def remove_odd_values(head: Optional[ListNode]) -> Optional[ListNode]: ``` ### Rules - `ListNode` has an integer field `val` and a field `next` that holds the following node, or `None` at the tail. - In the examples a list is written as its values from head to tail. `[]` is the empty list: the input `head` is `None`, and an empty result means returning `None`. - A value `v` is odd when `v % 2 != 0`. Negative odd values such as -3 are removed. Zero and negative even values such as -4 stay. ### Constraints - The number of nodes in the list is in the range `[0, 10^4]`. - `-10^5 <= Node.val <= 10^5` ### Examples ```text Input: head = [1, 2, 3, 4, 5, 6] Output: [2, 4, 6] ``` The odd values 1, 3 and 5 are removed, leaving `2 -> 4 -> 6` in the original order. ```text Input: head = [7, 7, 7] Output: [] ``` All values are odd, so the resulting list is empty and the function returns `None`. ```text Input: head = [-3, 0, -4, 5, 8] Output: [0, -4, 8] ``` The values -3 and 5 are odd and are removed. Zero and -4 are even, so they stay in place before 8.

Overview: Remove every node with an odd value from a singly linked list and return the new head, keeping the even values in their original order. It tests pointer updates when the head or a run of consecutive nodes must be removed, and correct parity checks for zero and negative values.

PracHub logo
General
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given the head of a singly linked list, remove all nodes whose value is odd, and return the new head of the resulting linked list.

A value is odd if it is not divisible by 2. Only nodes with even values (including zero and negative even numbers) should remain in the list, preserving their original order.

Function Signature

def remove_odd_values(head: Optional[ListNode]) -> Optional[ListNode]:

Rules

  • ListNode has an integer field val and a field next that holds the following node, or None at the tail.
  • In the examples a list is written as its values from head to tail. [] is the empty list: the input head is None , and an empty result means returning None .
  • A value v is odd when v % 2 != 0 . Negative odd values such as -3 are removed. Zero and negative even values such as -4 stay.

Constraints

  • The number of nodes in the list is in the range [0, 10^4] .
  • -10^5 <= Node.val <= 10^5

Examples

Input: head = [1, 2, 3, 4, 5, 6]
Output: [2, 4, 6]

The odd values 1, 3 and 5 are removed, leaving 2 -> 4 -> 6 in the original order.

Input: head = [7, 7, 7]
Output: []

All values are odd, so the resulting list is empty and the function returns None.

Input: head = [-3, 0, -4, 5, 8]
Output: [0, -4, 8]

The values -3 and 5 are odd and are removed. Zero and -4 are even, so they stay in place before 8.