leetcode[86]:Partition List

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值