FunctionRacerProblemsRace

Remove Nodes Outshone Nearby

HardLinked listsPython, JavaScript, TypeScript, Java, C++

A node is outshone when one of the k nodes right after it holds a strictly larger value. Near the end of the list there are fewer than k nodes after a node, and then only those count.

Remove every outshone node and return the head. All nodes are judged on the list as it is given, not on what is left after other removals. For 5 -> 2 -> 13 -> 3 -> 8 and k = 1 the nodes 2 and 3 go, because each stands right before a larger value. The list is passed in as head.

Linked lists appear in the tests as the list of their values, like [1,2,3]. Each node is a ListNode with .val and .next; the class is already defined for you.

Example 1

head [5,2,13,3,8]
k 1
returns [5,13,8]

Only the next node counts. 2 stands before 13 and 3 before 8, so both are removed.

Example 2

head [5,2,13,3,8]
k 2
returns [13,8]

With two nodes to look at, the 5 also has the larger 13 in reach.

Example 3

head [9,1,1,1,10]
k 3
returns [9,10]

From 9 the 10 is four nodes away, out of reach. The three 1s all have it in reach.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Linked lists problems