Difficult:Medium
題目
Given the head of a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.
You should preserve the original relative order of the nodes in each of the two partitions.
翻譯
給定鍊錶的頭部和值 x,將其分區,使得所有小於 x 的節點都在大於或等於 x 的節點之前。
您應該保留兩個分區中每個分區中節點的原始相對順序。
範例
Example 1:
1 | Input: head = [1,4,3,2,5,2], x = 3 |
Example 2:
1 | Input: head = [2,1], x = 2 |
解題思路
1.當小於x的node放到minhead裡
2.當大於等於的node放到maxhead裡
3 最後再將minhead和maxhead連結
Solution
1 | var partition = function (head, x) { |