LeetCode解题心得,欢迎交流! 第三日
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* partition(struct ListNode* head, int x) {
struct ListNode *dummy1=(struct ListNode *)malloc(sizeof(struct ListNode));
struct ListNode *dummy2=(struct ListNode *)malloc(sizeof(struct ListNode));
struct ListNode *cur1=dummy1;
struct ListNode *cur2=dummy2;
while(head!=NULL)
{
if(head->val < x)
{
cur1->next=head;
cur1=head;
}
else
{
cur2->next=head;
cur2=head;
}
head=head->next;
}
cur2->next=NULL;
cur1->next=dummy2->next;
return dummy1->next;
}