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 ...
随机推荐
- grpc入门(三)
grpc入门(三) 一.介绍 本文是关于grpc的第三篇博文,是对前两篇博文的具体代码实现,秉着个人一贯的风格,没有太多抒情和总结,直接就上代码. 文章代码参考:https://github.com/ ...
- win10+eclipse+hadoop2.7.2+maven+local模式直接通过Run as Java Application运行wordcount
一.准备工作 (1)Hadoop2.7.2 在linux部署完毕,成功启动dfs和yarn,通过jps查看,进程都存在 (2)安装maven 二.最终效果 在windows系统中,直接通过Run as ...
- 20145324 《Java程序设计》第10周学习总结
20145324 <Java程序设计>第10周学习总结 教材学习内容总结 1.网络编程的实质就是两个(或多个)设备(例如计算机)之间的数据传输 2.在实际传输数据以前需要将域名转换为IP地 ...
- c语言数据类型字节长度
突然间就想到了long和int到底什么区别(发现有很多问题都是突然间想到的),然后百度.google各种查找,各种书籍:<C++ Primer>.<C程序设计语言>查看,终于明 ...
- JAVA基础补漏--Collections工具类排序
Collections在对自定义对象进行排序时,自定义类需要对compareTo()函数进行重写. public class Student implements Comparable<Stud ...
- Feign PathVariable annotation was empty on param 0.
使用Feign的时候,如果参数中带有 @PathVariable形式的参数,则要用value=""标明对应的参数,否则会抛出IllegalStateException异常 如 @P ...
- 从0开始 Java学习 packet用法
packet 包的用法 参考博客:https://www.cnblogs.com/Ring1981/p/6240412.html 用法 java 源文件带有包名,往往容易出错 如:H:\code\He ...
- mac iterm2 打开Linux 服务器文件乱码
我的mac 上用是iterm2终端, Shell 环境是zsh.ssh 到Linux 服务器上查看一些文件时,中文乱码. 这种情况一般是终端和服务器的字符集不匹配,MacOSX下默认的是utf8字符集 ...
- Python基础笔记系列九:变量、自定义函数以及局部变量和全局变量
本系列教程供个人学习笔记使用,如果您要浏览可能需要其它编程语言基础(如C语言),why?因为我写得烂啊,只有我自己看得懂!! 变量在前面的系列中也许就可以发现,python中的变量和C中的变量有些许不 ...
- ubuntu16.04 安装power shell
ubuntu16.04 安装power shell # Download the Microsoft repository GPG keys wget -q https://packages.micr ...