leetcode 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.
Example:
1 | Input: head = 1->4->3->2->5->2, x = 3 |
- 把所有小于x的数放在x的左边,大于等于x的数放在右边。
- 从左往右依次遍历一遍数组,设置两个标记分别标记左边和右边
- 把小于x的放置到左边,把大于等于x的放置到右边
- 把左右两边连接起来
1 | /** |
- 注意最后的great.next一定要设置成空。不然对于这种情况:
1->2->3->6->4
会形成环