[LeetCode] 86. Partition List
来源:互联网 发布:交友聊天软件 编辑:程序博客网 时间:2024/06/18 18:17
[LeetCode] 86. 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.
思路:定义两个指针,一个负责链接小于x的值,另一个负责链接大于等于x的值,最后把两个链表连起来。
class Solution {public: ListNode* partition(ListNode* head, int x) { ListNode* l = new ListNode(-1); ListNode* r = new ListNode(-1); ListNode* rh = r; ListNode* lh = l; ListNode* p = head; while (p != NULL) { if (p->val < x) { l->next = p; l = l->next; } else { r->next = p; r = r->next; } p = p->next; } r->next = NULL; l->next = rh->next; return lh->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
- 86. Partition List LeetCode
- 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
- 实时分析系统以及hbase、hive和impala的区别
- JavaScript之DOM事件模型
- LeetCode部分题解
- Java基本数据类型及其封装器的一些千丝万缕的纠葛
- sed详解---基础篇
- [LeetCode] 86. Partition List
- logicTrace小游戏
- (1)Air Band调研
- [Haskell on HackerEarth] Monk and Digital World
- ssh无法连上虚拟机中linux
- nyoj 927 The partial sum problem
- 深度学习实践经验:用Faster R-CNN训练Caltech数据集——训练检测
- 二叉树的创建与遍历
- SQLite执行计划(Query Planning)---数据库索引篇