86. Partition List
来源:互联网 发布:热设计软件 编辑:程序博客网 时间:2024/05/01 09:55
方法1:插入&删除
class Solution {public: ListNode* partition(ListNode* head, int x) { ListNode* Head=new ListNode(-1); Head->next=head; ListNode* pre=Head,*cur=head,*p=pre; while(cur) { if(cur->val<x) { if(pre->next==cur) { p=pre=cur; cur=cur->next; } else { ListNode* nx=cur->next; cur->next=pre->next; pre=pre->next=cur; p->next=nx; cur=nx; } } else { p=cur; cur=cur->next; } } return Head->next; }};
方法2:
建立两个链表
class Solution {public: ListNode* partition(ListNode* head, int x) { ListNode* left=new ListNode(-1); ListNode* right=new ListNode(-1); ListNode* l=left,*r=right; ListNode* p=head; while(p) { if(p->val<x) l=l->next=p; else r=r->next=p; p=p->next; } r->next=NULL; l->next=right->next; return left->next; }};
0 0
- [LeetCode]86.Partition List
- LeetCode 86. Partition List
- [Leetcode] 86. Partition List
- LeetCode --- 86. Partition List
- 86. Partition List
- [leetcode] 86.Partition List
- [leetcode] 86. Partition List
- 86. Partition List
- [LeetCode]86. Partition List
- 86. Partition List
- 86. Partition List LeetCode
- 86. Partition List
- 86. Partition List
- 86. Partition List
- Leetcode 86. Partition List
- LeetCode *** 86. Partition List
- LeetCode 86. Partition List
- [LeetCode]86. Partition List
- ES5 中的面向对象编程前传 之 深刻理解原型链(prototype)
- flume高并发优化——(11)排除json转换及中文乱码
- Your build settings specify a provisioning profile with the UUID‘XXX’
- JAVA深入研究——Method的Invoke方法。
- 【ExpandableListView】---2 长按和点击处理
- 86. Partition List
- POJ 3468 Lazy标记
- 51nod oj 1272 最大距离【贪心】
- MySQL入门
- Arrayutils数组工具类的学习
- 数据库C语言API编写实例
- HashTable
- cookie到底是什么
- Android网络请求库 - Say hello to OkHttp