就是用兩個head 一個是大,一個是小,然後分別制定,一定要將Big->next = NULL; 否則會形成循環列表,超出時間限制。
class Solution {
public:
ListNode* partition(ListNode* head, int x) {
ListNode* DummySmall = new ListNode(0);
ListNode* DummyBig = new ListNode(0);
ListNode* Small = new ListNode(0);
ListNode* Big = new ListNode(0);
DummySmall->next = Small;
DummyBig->next = Big;
while(head != NULL){
if(head->val < x){
//ListNode* SmallNew = new ListNode(head->val);
Small->next = head;
Small = Small->next;
}
else{
//ListNode* BigNew = new ListNode(head->val);
Big->next = head;
Big = Big->next;
}
head = head->next;
}
Big->next = NULL;
Small->next = DummyBig->next->next;
return DummySmall->next->next;
}
};