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 ...
随机推荐
- lapis 处理接收到的json 数据
备注: 在restful api 开发过程中,大家一般使用的都是json 格式的数据lapis 在处理json 数据上也是比较方便的 1. 使用的api 说明 local ...
- 【转】python 字符编码与解码——unicode、str和中文:UnicodeDecodeError: 'ascii' codec can't decode
原文网址:http://blog.csdn.net/trochiluses/article/details/16825269 摘要:在进行python脚本的编写时,如果我们用python来处理网页数据 ...
- ubuntu 12.04lts 安装mysql ,并通过QT连接
安装server$ sudo apt-get install mysql-server 安装驱动 $ sudo apt-get install libqt4-sql-mysql $ dpkg --li ...
- appium+python自动化40-adb offline(5037端口被占)
前言 adb连手机的时候经常会出现offline的情况,一般杀掉adb,然后重启adb可以解决. 如果发现不管怎么重启adb都连不上,一直出现offlie的情况,这个时候很大可能就是adb的5037端 ...
- [Java][Web]Request 获取请求头和数据
获取方式一 InputStream in = request.getInputStream(); int len = 0; byte buffer[] = new byte[1024]; while( ...
- Centos6-7安装Python3.5以及SSL的编译安装,识别https
Python3中无法导入ssl模块的解决办法 如果你发现在python3脚本运行过程中发现涉及到ssl模块都无法运行的情况下.那么需要进行如下步骤 第一步: yum install openssl o ...
- 不能调用jquery中ready里面定义的函数?
现象:不能调用jquery中ready里面定义的函数 源码:<script type="text/javascript"> $(document).ready(func ...
- 第六章 通过Service访问Pod(下)
6.4 外网如何访问service (1)ClusterIp: Service通过Cluster内部的IP对外提供服务,只有Cluster内的节点和Pod可以访问,这是默认的Service类型. (2 ...
- Tkinter学习
from tkinter import * window = Tk() # 创建一个窗口 window.mainloop() # 消息循环,显示窗口 window.title("窗口标题&q ...
- CentOS 7 安装Nginx 并配置自动启动
1.官网下载安装包 http://nginx.org/en/download.html,选择适合Linux的版本,这里选择最新的版本,下载到本地后上传到服务器或者centos下直接wget命令下载. ...