86. Partition List

Given 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.

For example,

Given 1->4->3->2->5->2 and x = 3,

return 1->2->2->4->3->5.

给一个链表,和一个整数x, 要求把所有比x小的节点放到前边,把比x大的节点放到后边。


代码:


86. Partition List_第1张图片
参考代码

解题思路:弄一个 less链表和greate链表,遍历head链表,对应的大小值分别给less和greater链表,最后把greate链表链接在less 的后边。

你可能感兴趣的:(86. Partition List)