ListNode * small=new ListNode(0); ListNode * big=new ListNode(0); ListNode * first=new ListNode(0); ListNode * second=new ListNode(0); first=small; second=big; while(head->next!=NULL) { if(head->val<=val) { small->next=head; small=small->next; } else { big->next=head; big=big->next; } head=head->next; } if(head->val<=val) { small->next=head; small=small->next; small->next=NULL; } else { big->next=head; big=big->next; big->next=NULL; } if(first->next==NULL)return second->next; else if(second->next==NULL)return first->next; else { small->next=second->next; return first->next; }
第二次提交的代码,正确通过。
if(head==NULL)return NULL; ListNode * small=NULL; ListNode * big=NULL; ListNode * first=NULL; ListNode * second=NULL; while(head) { if(head->val<=val) { if(first==NULL) { first=head; small=head; } else { small->next=head; small=head; } } else { if(second==NULL) { second=head; big=head; } else { big->next=head; big=head; } } head=head->next; } if(first==NULL) { big->next=NULL; return second; } else if(second==NULL){ small->next=NULL; return first; } else { big->next=NULL; small->next=second; return first; } }