标签:
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* partition(ListNode* head, int x) { if(head == NULL || head->next ==NULL) return head; ListNode* head1=new ListNode(0); ListNode* head2=new ListNode(0); ListNode* t1,*t2; head1->next=head; head2->next=head; t1=head1,t2=head2; while(head) { ListNode* tem; if(head->val < x) { tem=head; t1->next=tem; t1=t1->next; } else { tem=head; t2->next=tem; t2=t2->next; } head=head->next; } t2->next=NULL; //必须要加上这句,否则可能是死循环。 t1->next=head2->next; return head1->next; } };
标签:
原文地址:http://www.cnblogs.com/yanqi110/p/5004850.html