86. Partition List
来源:互联网 发布:算法分析 分治法总结 编辑:程序博客网 时间:2024/05/16 06:09
Given a linked list and a value x, partition it such that all nodes less thanx 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
.
Subscribe to see which compa
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* partition(ListNode* head, int x) { ListNode* pre=NULL; ListNode* sec=NULL; ListNode* root=NULL; ListNode* p = NULL; while(head) { ListNode* post =head->next; if(head->val < x) { if(pre==NULL) { pre=head; root=pre; } else { pre->next=head; pre= pre->next; } } else { if(sec==NULL) { sec=head; p=sec; } else { sec->next = head; sec=sec->next; } } head=post; } if(sec) { sec->next=NULL; } if(pre) { pre->next=p; return root; } return p; }};
nies asked this question
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
- 关于android串口通信
- 简单实现Android搜索功能 显示清除历史搜索记录
- Graylog——日志聚合工具的后起之秀
- Android学习第一天
- Matrix 和 ColorMatrix详细分析
- 86. Partition List
- spring定时器使用心得
- POJ 1209 模拟进栈和出栈
- iOS浅拷贝和深拷贝的区别
- Super关键字
- Html-框架
- 我为什么不再做PL人
- 运维日记007 - vim操作总结(三、可视化模式与多窗口)
- C语言数据内存