[Linked List]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) {
ListNode* pivot = NULL;
ListNode* cur = head;
ListNode* insert_pos = cur;
ListNode* insert_pos_pre = NULL;
while(cur && cur->val < x){
insert_pos_pre = cur;
cur = cur->next;
insert_pos = cur;
}
pivot = cur; /*
pivot 左边做插入,insert_node_pre,inser_node
pivot 右边做删除, delete_node_pre,delete_node
*/ ListNode* possible_delete_node_pre = pivot;
ListNode* possible_delete_node = pivot ? pivot->next : NULL; while(possible_delete_node){
ListNode* possible_delete_node_next = possible_delete_node->next;
if(possible_delete_node->val < x){
if(insert_pos_pre){
insert_pos_pre->next = possible_delete_node ;
}else{
head = possible_delete_node;
}
possible_delete_node->next = insert_pos; insert_pos_pre = possible_delete_node;
insert_pos = insert_pos_pre->next; possible_delete_node_pre ->next = possible_delete_node_next; }else{
possible_delete_node_pre = possible_delete_node;
}
possible_delete_node = possible_delete_node_next;
} return head; }
};
[Linked List]Partition List的更多相关文章
- [LeetCode] Partition List 划分链表
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- leetcode 86. Partition List
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- 【leetcode】Partition List
Partition List Given a linked list and a value x, partition it such that all nodes less than x come ...
- 【leetcode】Partition List(middle)
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- Leetcode Partition List
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- 46. Partition List
Partition List Given a linked list and a value x, partition it such that all nodes less than x come ...
- [CareerCup] 2.4 Partition List 划分链表
2.4 Write code to partition a linked list around a value x, such that all nodes less than x come bef ...
- [LeetCode]题解(python):086 - Partition List
题目来源 https://leetcode.com/problems/partition-list/ Given a linked list and a value x, partition it s ...
- 86. Partition List
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
随机推荐
- Linux脚本中使用特定JDK
有时linux系统中装了很多应用,我们又不能覆盖系统中设置的版本,此时我们就需要在脚本文件中设置特定版本. export JAVA_HOME= export CLASSPATH=.:$JAVA_HOM ...
- [转载]Linux 性能监控、测试、优化工具
Linux 平台上的性能工具有很多,眼花缭乱,长期的摸索和经验发现最好用的还是那些久经考验的.简单的小工具.系统性能专家 Brendan D. Gregg 在最近的 LinuxCon NA 2014 ...
- VLC的相关文档以及javascript接口
参看下面链接:VLC相关文档
- centos源码安装git
因为Centos上yum安装的话可能版本比较低,使用中会有一些难以预料的问题出现. 从源代码编译安装方法: #Centos执行: yum install curl-devel expat-devel ...
- Mysql SlowLog 工具 pt-query-diglist
工具地址:http://www.percona.com/doc/percona-toolkit/ 安装依赖工具 yum install perl-Time-HiRes 编译安装 perl Makefi ...
- Lammp安装过程
-1 建设环境 创建mysql数据库的lvm环境 创建lv逻辑卷 新安装一个硬盘25G sdb #fd ...
- LaTeX使用titlesec宏包改变章节编号形式的方法
1.titleformat宏包命令详解 LaTeX中可以用titlesec宏包中的titleformat命令来改变标题形式: 导入宏包: \usepackage{titlesec} 改变标题的代码如下 ...
- Dividing (hdu 1059 多重背包)
Dividing Sample Input 1 0 1 2 0 0 价值为1,2,3,4,5,6的物品数目分别为 1 0 1 2 0 0,求能否将这些物品按价值分为两堆,转化为多重背包.1 0 0 0 ...
- MVC中配置OutputCache的VaryByParam参数无效的问题
在项目使用OutputCacheAttribute是遇到了问题,当我想在配置文件web.config中配置OutputCache的VaryByParam时竟然不起作用,下面是相关代码: 文件FaceC ...
- 从Qt4到Qt5的,主要的进化有三(对于QtWidget的精简和优化会很有限)
从Qt4到Qt5的,主要的进化有三:1 语言的进化,原来是基于C++(qtwidget)和XML(.ui),现在添加了QML(QtQuick)+JS(v8)的架构.2 绘图系统的进化,原先基于QPai ...