题目描述:
解题思路:
遍历链表,按照大小分成两个链表,然后将两个合并。
参考代码:
public ListNode partition(ListNode head, int x) {
ListNode smallHead=new ListNode();
ListNode bigHead=new ListNode();
ListNode smallTail=smallHead;
ListNode bigTail=bigHead;
while (head!=null)
{
if(head.val<x)
{
smallTail=smallTail.next=head;
}
else
{
bigTail=bigTail.next=head;
}
head=head.next;
}
smallTail.next=bigHead.next;
bigTail.next=null;
return smallHead.next;
}