/**
* 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;
}
};
转载于:https://www.cnblogs.com/yanqi110/p/5004850.html
相关资源:数据结构—成绩单生成器