86. Partition List (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) {
if(!head || !(head->next) ) return head;
ListNode *current = head;
ListNode *smallPointer = NULL; //point to the last node <x
ListNode *largePointer = NULL; //point to the last node >x
while(current)
{
if(current->val >= x)
{
largePointer = current;
current = current->next;
}
else
{
if(!largePointer)
{
smallPointer = current;
current = current->next;
}
else if(smallPointer)
{
largePointer->next = smallPointer->next;
smallPointer -> next = current;
current = current->next;
smallPointer = smallPointer->next;
smallPointer->next = largePointer->next;
largePointer->next = current;
}
else //head
{
smallPointer = current;
current = current->next;
smallPointer->next = head;
head = smallPointer;
largePointer->next = current;
}
}
}
return head;
}
};
86. Partition List (List)的更多相关文章
- leetcode 143. Reorder List 、86. Partition List
143. Reorder List https://www.cnblogs.com/grandyang/p/4254860.html 先将list的前半段和后半段分开,然后后半段进行逆序,然后再连接 ...
- 【LeetCode】86. Partition List 解题报告(Python)
[LeetCode]86. Partition List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...
- 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 ...
- 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 解题思路
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- LeetCode OJ 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
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 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
Question: Given a linked list and a value x, partition it such that all nodes less than x come befor ...
随机推荐
- phoenxi elixir 框架几个方便的命令
1. 已有命令 mix app.start # Starts all registered apps mix app.tree # Prints the application tree mix ar ...
- apache phoenix 安装试用
备注: 本次安装是在hbase docker 镜像的基础上配置的,主要是为了方便学习,而hbase搭建有觉得 有点费事,用镜像简单. 1. hbase 镜像 docker pull har ...
- 微软Enterprise Library 4.1和Unity 1.2
说明 微软模式与实践团队今天发布了Enterprise Library 4.1和Unity 1.2版本,这次发布的主要新特性如下: 1. 支持Visual Studio 2008 SP1 2. Uni ...
- 腾讯加入QQ群,代码生成地址
腾讯加入QQ群,代码生成地址 http://qun.qq.com/join.html
- kubernetes简单示例
1. 安装 yum install -y etcd kubernetes 2. 启动 systemctl start etcd systemctl start docker systemctl sta ...
- 2.JMeter查看结果树返回编码格式Unicode转为中文方法
在使用JMeter做接口测试时,发现相同url,用postman工具,其返回数据参数为中文,而用JMeter工具,其返回参数为Unicode,如下图所示 解决方法如下: 1.Jmeter在对应的请求上 ...
- SublimeText3搭建go语言开发环境(windows)
SublimeText3搭建go语言开发环境(windows) 下载并解压: Sublime Text Build 3021.zip注册: 尽量不要去破解 安装Package C ...
- 软RAID 0的技术概要及实现
1 什么是RAID,RAID的级别和特点 : 什么是RAID呢?全称是 “A Case for Redundant Arrays of Inexpensive Disks (RAID)”,在1987年 ...
- Java中的自动类型转换/隐式类型转换
整型.实型(常量).字符型数据可以混合运算.运算中,不同类型的数据先转化为同一类型,然后进行运算. 转换从低级到高级. 自动类型转换必须满足转换前的数据类型的位数要低于转换后的数据类型,例如: sho ...
- mysql 优化(2)
--cfq,deadline,noop--radi10,xfs,ext4--innodb_flush_log_at_trx_commit=0/1/2--sys_binlog=1/n CPU的利用特点• ...