FunctionRacerProblemsRace

Partition List

MediumLinked listsPython, JavaScript, TypeScript, Java, C++

Rearrange the list so that every node with a value less than x comes before every node with a value greater than or equal to x. Inside each of the two groups, keep the original order. Return the head. 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 [1,4,3,2,5,2]
x 3
returns [1,2,2,4,3,5]

Smaller than 3: 1, 2, 2. The rest: 4, 3, 5.

Example 2

head [2,1]
x 2
returns [1,2]

1 is the only value below 2.

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