LeetCode OJ: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.
将链表分割成两部分,大于某个数字的在左侧,小于等于某个数字的在右侧,用的方法比较蠢可能就是遍历一次分成两个链表,然后再将它们接起来。具体代码如下所示:
class Solution {
public:
ListNode* partition(ListNode* head, int x) {
ListNode * helper1 = new ListNode(INT_MIN);
ListNode * helper2 = new ListNode(INT_MIN);
ListNode * p1 = helper1;
ListNode * p2 = helper2;
while(head){
if(head->val < x){
p1->next = head;
head = head->next;
p1 = p1->next;
p1->next = NULL;
}else{
p2->next = head;
head = head->next;
p2 = p2->next;
p2->next = NULL;
}
}
p1->next = helper2->next;
return helper1->next;
}
};
LeetCode OJ:Partition List(分割链表)的更多相关文章
- [LeetCode] 763. Partition Labels 分割标签
A string S of lowercase letters is given. We want to partition this string into as many parts as pos ...
- [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 86. Partition List 划分链表 C++
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]86. Partition List分离链表
/* 这个题是medium的意思应该是用双指针的方法做,如果使用下边的新建链表的方法,就是easy的题目了 双指针会用到很多链表的相连操作 */ public ListNode partition(L ...
- 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:分割链表【86】
LeetCode:分割链表[86] 题目描述 给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前. 你应当保留两个分区中每个节点的初始相对位置. 示例 ...
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- [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: Palindrome Partition
LeetCode: Palindrome Partition Given a string s, partition s such that every substring of the partit ...
随机推荐
- CCTableView
今天用到TableView, 我就来记录一下....这些都是在网上找到了资料 //首先 继承 : public cocos2d::extension::CCTableViewDelegate,publ ...
- php AES-128-CBC 加密 通信java
<?phpheader("Content-type: text/html; charset=utf-8");require('MyAES.php');class Xfb{ / ...
- 分享个基于 Node.js + React 的博客系统
是使用 ES2015+ 特性写的,使用了 ThinkJS 框架,后台使用了 React. 完全使用 Markdown 来写文章,还可以把文章推送到团队博客系统中(团队博客也需要使用该系统). 项目地址 ...
- oracle错误一览表
ORA-00001: 违反唯一约束条件 (.)ORA-00017: 请求会话以设置跟踪事件ORA-00018: 超出最大会话数ORA-00019: 超出最大会话许可数ORA-00020: 超出最大进程 ...
- ThinkPHP5显示数据库字段内容
1.在application文件夹下面的config.php中打开DEBUG. 2.修改tp5/application/index/controller/Index.php内容. <?php n ...
- 20145310 《Java程序设计》第4周学习总结
20145310 <Java程序设计>第4周学习总结 教材学习内容总结 本周主要进行第五章和第六章的学习. 第六章 继承与多态 子类(Inherit)继承父类,避免重复的行为定义,不过并非 ...
- 20145331 实验一 "Java开发环境的熟悉"
20145331 实验一 "Java开发环境的熟悉" 实验内容 使用JDK和IDE编译.运行简单的Java程序.题目: 实现四则运算,并进行测试. 编写代码 1.首先第一步就是要输 ...
- SpringMVC对于传入多个对象参数遇到的问题
最近遇到一个问题,一个添加接口,需要添加三个对象,而且这三个对象里面的属性名很多都是一样的,本来是拿三个对象直接接收值,但是因为很多属性名都一样,所以接收不到值.百度也有的说把这三个对象的参数重命名然 ...
- 1_archlinux_安装篇
系统安装 可参考网友的文章: https://www.douban.com/note/263375303/?type=like http://www.68idc.cn/help/server/linu ...
- 物料类型AM11没有任务清单类型N定义
CA01 创建工艺路线时报错信息:“物料类型AM11没有为任务清单类型N定义” (如下图) 处理方法: 配置路径:生产->基本数据->工艺路线->通用数据->定义物料类型分配 ...