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
- LeetCode 86: Partition List
- LeetCode 86 Partition List
- [leetcode 86] Partition List
- leetcode-86 Partition List
- [LeetCode 86]Partition List
- leetcode || 86、Partition List
- LeetCode---(86) Partition List
- LeetCode 86 Partition List
- Leetcode[86]-Partition List
- leetcode[86]:Partition List
- Leetcode #86 Partition List
- LeetCode(86) Partition List
- leetcode 86: Partition List
- [Leetcode]#86 Partition List
- leetCode #86 Partition List
- leetcode 86:Partition List
- 【leetcode】【86】Partition List
- leetcode-Partition List-86
- mcafee8.8 Patch1与mcafee8.7 Patch5右键扫描出现msi.dll等无效映像错误的解决方法
- 经典DFS--找油田---POJ 1562
- MPI_Gathe MPI收集
- 在Ubuntu14.04下编译caffe + cuda7.0
- Dev-Cpp/Mingw32 环境介绍
- leetcode[86]:Partition List
- 近期使用McAfee8.8时碰到的一些问题
- 真情妙悟铸文章
- 简单的桶排序
- CC2540/CC2541 : Set the Peripheral Being Advertising while It is Being Connected
- 覆盖率(白盒)测试
- Java文件拷贝
- PDF格式的图纸如何转换成CAD格式
- Android Canvas的save(),saveLayer()和restore()浅谈