[LeetCode]86. Partition List
来源:互联网 发布:淘宝五分钟内拍下 编辑:程序博客网 时间:2024/05/21 10:25
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.
/** * 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) { if (head == NULL) return NULL; ListNode* pCur = head; ListNode* left = new ListNode(-1); ListNode* right = new ListNode(-1); ListNode* l = left; ListNode* r = right; while (pCur){ if (pCur->val < x){ l->next = pCur; l = l->next; //pCur_left = NULL; } else{ r->next = pCur; r = r->next; } pCur = pCur->next; } // r的next指针必须置为0,因为它是right的最后一个结点 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
- [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
- Redis安装
- C++智能指针
- IELTS11 Test4 besides economy, other types of progress are equally important
- 《Java并发编程的艺术》笔记三——锁的升级与对比.md
- RecyclerView系列(4)—XRexyclerView的坑,java.lang.IndexOutOfBoundsException: Inconsistency detected
- [LeetCode]86. Partition List
- python (小)模块简介
- 利用Sympy做数学
- ps的一些使用技巧
- Linux(十四):磁盘分区与格式化
- POJ 1637 网络流判混合欧拉回路 解题报告
- Python3学习(11)--高阶函数
- Eclipse-properties文件
- 小辨常量指针与指向常量的指针