Skip to the code
cf.
+25 XP

#228 Split Logs Around a Severity

medium

A log panel holds entries in a linked list, and each node's val is the entry's severity score. To make the quiet entries easy to skim, the panel moves every entry with a score lower than limit to the front.

Write splitBySeverity(head, limit) that reorders the nodes so all nodes with val < limit come before all nodes with val >= limit. Inside each of the two groups, keep the original order. Return the new head.

Example 1

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

1, 2, 2 are below 3 and keep their order; 4, 3, 5 follow in their order.

Example 2

Input: head = [2,1], limit = 2
Output: [1,2]

Only 1 is below 2, so it moves in front of 2.

Constraints

  • The list has 0 to 200 nodes.
  • -100 <= node.val, limit <= 100

Lists are written as arrays here, like [1, 2, 3]. Your function works with ListNode objects, each with val and next.

Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).