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 ...
随机推荐
- Log4Net 日志文件分类保存
1.app.config <configSections> <section name="log4net" type="log4net.Config.L ...
- 有道云笔记配合MPic+七牛云 自制MarkDown文档图床(适用Typora)
注:从有道云笔记v6.5开始,有道云笔记会员可以使用MarkDown有道自带的图床.(但是非会员可以采用下面的七牛云图床+MarkDown方法) 0x00 前言 一直用有道云笔记,粘贴图片,做笔记没问 ...
- [Linux 002]——Linux的常用命令
经过前面的学习,大概了解了计算机组成原理和操作系统的一些知识.尽管这些知识都是琐碎的,拼凑的,在以后的工作和学习中仍需进行深入的了解.但是这些预备知识对于准备跨入 Linux 大门的童鞋来说,应该是足 ...
- 20145321 《Java程序设计》第6周学习总结
20145321 <Java程序设计>第6周学习总结 教材学习内容总结 第十章 输入输出 10.1 InputStream OutputStream 1.数据有来源与目的,衔接两者的是串流 ...
- ubuntu18.04安装ssh服务
1.安装openssh-server sudo apt-get install openssh-server 2.启动ssh服务 sudo service ssh start 3.检测是否启动了ssh ...
- SaltStack本地管理无master模式-第八篇
Salt本地管理应用场景 1.在边缘节点服务器非常少没有Salt-master 2.零售店,电影院等弱网络环境没有Salt-master 3.快速部署单个服务没有Salt-master 实现 一,安装 ...
- LeetCode——Sort List
Question Sort a linked list in O(n log n) time using constant space complexity. Solution 分析,时间复杂度要求为 ...
- 线程实现Runnable接口比继承Thread的优势
1.适合多个相同程序代码的线程去处理同一资源,把虚拟CPU(线程)同程序的代码.数据有效分离,较好地体现了面向对象的设计思想.2.可以避免由于java单继承特性带来的局限.3.增强了程序的健壮性,代码 ...
- 第四篇:Spark SQL Catalyst源码分析之TreeNode Library
/** Spark SQL源码分析系列文章*/ 前几篇文章介绍了Spark SQL的Catalyst的核心运行流程.SqlParser,和Analyzer,本来打算直接写Optimizer的,但是发现 ...
- You only look once
计算MAP https://www.zhihu.com/question/53405779 http://tarangshah.com/blog/2018-01-27/what-is-map-unde ...