FastPrepFastPrep
Problem Brief

Delete Nodes Greater Than X

OA

Given a singly linked list and an integer, x, remove nodes greater than x.

Example:
List = 100 → 105 → 50
x = 100
List becomes 100 → 50

Return a reference to the root node of the list after removing 105.

Function Description

Complete the function removeNodes in the editor with the following parameter(s):

  • node listHead: a reference to the root node of the singly-linked list
  • int x: the maximum value to be included in the returned singly-linked list

Returns:

node: a reference to the root node of the final list

Constraints

  • 1 ≤ n, x ≤ 105
  • 1 ≤ SinglyLinkedListNode values ≤ 105

1Example 1

Input
listHead = 100 → 105 → 50, x = 100
Output
100 → 50
Explanation

The nodes of the list are traversed and any node with a value greater than x is removed. In this case, the node with value 105 is greater than x and is therefore removed, resulting in the final list: 100 → 50.

Constraints

Limits and guarantees your solution can rely on.

1 ≤ n, x ≤ 105
1 ≤ SinglyLinkedListNode values ≤ 105
class SinglyLinkedListNode {
    int data;
    SinglyLinkedListNode next;

    SinglyLinkedListNode(int nodeData) {
        this.data = nodeData;
        this.next = null;
    }
}

public SinglyLinkedListNode removeNodes(SinglyLinkedListNode listHead, int x) {
    // write your code here
}
Input

listHead

100 → 105 → 50

x

100

Output

100 → 50

Sign in to submit your solution.