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.
把数组分成两部分,然后连接在一起
public ListNode partition(ListNode head, int x) {
if(head == null || head.next == null) return head;
ListNode big = new ListNode(0),rbig = big; //大于等于x的部分
ListNode small = new ListNode(0),rsmall = small; //小于x的部分
ListNode cur = head;
while(cur != null){
if(cur.val < x){
small.next = cur;
small = small.next;
}
else{
big.next = cur;
big = big.next;
}
cur = cur.next;
}
small.next = rbig.next; //两部分连接
big.next = null;
return rsmall.next;
}