/*
* 86. Partition List
* 11.26 by Mingyang
* 没有什么诀窍,就是分成两个list,一个装小的一个装大的
* 只是注意small.next = head;赋值要准确,不能够错误,不能small=head
*/
public ListNode partition(ListNode head, int x) {
if (head == null || head.next == null)
return head;
ListNode small = new ListNode(-1);
ListNode newsmallhead = small;
ListNode big = new ListNode(-1);
ListNode newbighead = big;
while (head != null) {
if (head.val < x) {
small.next = head;
small = small.next;
} else {
big.next = head;
big = big.next;
}
head = head.next;
}
big.next = null;
small.next = newbighead.next;
return newsmallhead.next;
}