实现链表分割
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */structListNode*partition(structListNode*head,intx){structListNode*list1,*head1,*list2,*head2;list1=head1=(structListNode*)malloc(sizeof(structListNode));list2=head2=(structListNode*)malloc(sizeof(structListNode));structListNode*cur=head;while(cur){if(cur->val<x){list1->next=cur;list1=list1->next;}else{list2->next=cur;list2=list2->next;}cur=cur->next;}list2->next=NULL;list1->next=head2->next;head=head1->next;free(head1);free(head2);returnhead;}/**
- Definition for singly-linked list.
- struct ListNode {
int val;struct ListNode *next;- };
/
struct ListNodepartition(struct ListNode* head, int x)
{
struct ListNode* list1,head1,list2,head2;
list1=head1=(struct ListNode)malloc(sizeof(struct ListNode));
list2=head2=(struct ListNode)malloc(sizeof(struct ListNode));
struct ListNodecur=head;
while(cur)
{
if(cur->val<x)
{
list1->next=cur;
list1=list1->next;
}
else
{
list2->next=cur;
list2=list2->next;
}
cur=cur->next;
}
list2->next=NULL;
list1->next=head2->next;
head=head1->next;
free(head1);
free(head2);
return head;
}