问题描述:
Given a linked list and a value x, partition it such that all nodes less thanx 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
.
问题来源:Partition List (详细地址:https://leetcode.com/problems/partition-list/description/)
思路分析:这道题,我们就简单点,另外声明两个链表,一个用来数值小于x的链表,另一个链表的数值大于等于x。最后将两个链表合并起来就是了。其他的技巧啥的倒是没有,小细节不过挺多的,比如我们连接两个链表的时候要跨过头结点,最后返回的链表也要跳过头结点。
代码:
结点定义:
主体部分: