leetcode[86]:Partition List
2015-07-26 21:26
513 查看
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.
head:小数头指针
tail:大数头指针
head1:小数头移指针
tail1:大数头移动指针
flag:01小无头大有头(head未赋值,tail已赋值)
10小有头大无头(head已赋值,tail未赋值)
11小有头大有头(head已赋值,tail已赋值)
做的很复杂,运行时间也很长12ms,需要改进算法。
改进后:
两个虚头链表,一个放小数,一个放大数。去头合并即可。
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; }
两个虚头链表,一个放小数,一个放大数。去头合并即可。
相关文章推荐
- JAVA LinkedList和ArrayList的使用及性能分析
- LinkedList学习示例模拟堆栈与队列数据结构
- ArrayList和LinkedList的主要区别
- Java数据结构-线性表之单链表LinkedList
- ARRAYLIST VECTOR LINKEDLIST 区别与用法(2014-10-14)
- Remove Duplicates from Sorted List II
- Reverse Linked List
- Reverse Linked List II
- Partition List
- android性能之List
- Remove Nth Node From End of List
- Remove Duplicates from Sorted List
- Merge Two Sorted Lists
- ArrayList Vector LinkedList 区别与用法
- 黑马程序员————List集合概述、Arrayist集合、Vector集合、LinkedList集合简述
- 单向链表LinkedList的使用
- [Leetcode题解]002 Add Two Numbers
- LinkedList与ArrayList的区别
- Activity回退研究
- Java-Intersection of Two Linked Lists