leetcode[86]:Partition List

来源:互联网 发布:程序员都在这里接私活 编辑:程序博客网 时间:2024/05/01 21:49

Partition List

Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.

You should preserve the original relative order of the nodes in each of the two partitions.

For example,
Given 1->4->3->2->5->2 and x = 3,
return 1->2->2->4->3->5.

/** * Definition for singly-linked list. * struct ListNode { *     int val; *     struct ListNode *next; * }; */struct ListNode* partition(struct ListNode* head, int x) {    int flag=0;     struct ListNode* tail,*tmp,*head1,*tmp2,*tail1;    if(!head) return NULL;    if(head->val<x)    {        head1=head;        tmp=head->next;        head1->next=NULL;        flag=10;    }    else    {        tail=head;        tail1=head;        flag=1;        tmp=head->next;        tail1->next=NULL;    }    while(tmp)    {        if(tmp->val<x)        {            if(flag==10)             {                head1->next=tmp;                head1=head1->next;                tmp=tmp->next;            }            else if(flag==1)            {                head1=tmp;                tmp2=tmp->next;                head1->next=tail;                tmp=tmp2;                head=head1;                flag=11;            }            else             {                tmp2=tmp->next;                head1->next=tmp;                head1=head1->next;                head1->next=tail;                tmp=tmp2;            }        }        else        {            if(flag==10)             {                tmp2=tmp->next;                tmp->next=NULL;                tail=tmp;                tail1=tail;                 head1->next=tail1;                tmp=tmp2;                flag=11;            }            else if(flag==01)            {                tmp2=tmp->next;                tmp->next=NULL;                tail1->next=tmp;                tail1=tail1->next;                tmp=tmp2;            }            else            {                tmp2=tmp->next;                tail1->next=tmp;                tail1=tail1->next;                tail1->next=NULL;                tmp=tmp2;            }        }    }    return head;}

head:小数头指针
tail:大数头指针
head1:小数头移指针
tail1:大数头移动指针

flag:01小无头大有头(head未赋值,tail已赋值)
10小有头大无头(head已赋值,tail未赋值)
11小有头大有头(head已赋值,tail已赋值)

做的很复杂,运行时间也很长12ms,需要改进算法。

改进后:

/** * Definition for singly-linked list. * struct ListNode { *     int val; *     struct ListNode *next; * }; */struct ListNode* partition(struct ListNode* head, int x) {    struct ListNode *head0,*head1,*tmp,*tmp1,*tmp2,*tmp0,*tape=0;    if(!head) return NULL;    if(!head->next) return head;    head0=(struct ListNode *)malloc(sizeof(struct ListNode *));    head0->val=0;    head0->next=NULL;    head1=(struct ListNode *)malloc(sizeof(struct ListNode *));    head1->val=0;    head1->next=NULL;    tmp1=head0;    tmp2=head1;    while(head)    {         if(head->val<x)         {            tmp0=head->next;            head->next=NULL;            tmp1->next=head;            tmp1=tmp1->next;            head=tmp0;         }        else        {            tmp0=head->next;            head->next=NULL;            tmp2->next=head;            tmp2=tmp2->next;            head=tmp0;         }      }    tmp1->next=head1->next;    return head0->next;}

两个虚头链表,一个放小数,一个放大数。去头合并即可。

0 0
原创粉丝点击