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 基本开发
1. 生成项目代码 // 支持lua 以及 moonscript, 默认是moonscript 通过--lua 可以生成lua 的代码 lapis new --lua ├── app.lua ├── ...
- idea创建文件类型失败(xml之类的失效
https://blog.csdn.net/sutongxuevip/article/details/72832754
- c# 数据库通用类DbUtility
DbProviderType数据库类型枚举 /// <summary> /// 数据库类型枚举 /// </summary> public enum DbProviderTyp ...
- js中去掉字符串中的某个指定字符
假设一个data里面的数据是[tian,12],现在去掉[],代码如下 data=data.replace("[",""); data=data.replace ...
- Unit07: 状态管理-Session
Unit07: 状态管理-Session web package web; import java.io.IOException; import java.io.PrintWriter; import ...
- 数组与指针的区别,以及在STL中传递数组/指针
数组和指针在作为实参传入T[] 或T*的形参时没有区别 void f(int pi[]) { cout << sizeof(pi) << endl; } int a[5] = ...
- css怎么设置2个div同行,第一个固定宽度,第二个占满剩余的部分
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- java 堆、栈、常量池等
寄存器:我们在程序中无法控制 栈:存放基本类型的数据和对象的引用,但对象本身不存放在栈中,而是存放在堆中 堆:存放用new产生的数据 静态域:存放在对象中用static定义的静态成员 常量池:存放常量 ...
- cocostudio使用plist创建序列帧动画图片名称序列错乱的问题
cocostudio 版本v2.2.9 用texturePacker将动画帧打包成一个plist和一张png,将plist拖入cocostudio中的资源栏中. 如图所示,plist里面的图片顺序是乱 ...
- linux grep日志查询
ll access.2018-09-*.gz zcat access.2018-09-*.gz |grep --color '1073011900' | head -n 100 匹配字符由于管道h ...