系列文章目录
题目描述
代码
class Partition {
public:
ListNode* partition(ListNode* pHead, int x) {
struct ListNode *head1=(struct ListNode *)malloc(sizeof(struct ListNode));
struct ListNode *tail1=head1;
head1->next=NULL;
struct ListNode *head2=(struct ListNode *)malloc(sizeof(struct ListNode));
struct ListNode *tail2=head2;
head2->next=NULL;
while(NULL!=pHead)
{
if(pHead->val<x)
{
tail1->next=pHead;
tail1=tail1->next;
pHead=pHead->next;
tail1->next=NULL;
}
else
{
tail2->next=pHead;
tail2=tail2->next;
pHead=pHead->next;
tail2->next=NULL;
}
}
tail1->next=head2->next;
free(head2);
struct ListNode *temp=head1;
temp=temp->next;
free(head1);
return temp;
}
};