【一天一道LeetCode】#86. Partition List
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
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.
(二)解题
题目大意:给定一个链表和一个值,让小于给定值的结点都移到链表前面来,但不能改变这些数在原链表中的相对位置。
解题思路:用两个指针,一个指向小于给定值的链表部分的尾结点,一个用于遍历链表。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* partition(ListNode* head, int x) {
if(head==NULL||head->next==NULL) return head;
ListNode* lessTail = head;//小于x部分的尾节点
ListNode* p = head;
ListNode* pre = NULL;//p前面的节点
bool isFirst = false;//如果头节点就大于x为true,反之false
bool needToChange = false;//p前面的节点都小于x,则不需要改变,反之需要进行插入操作
if(head->val >= x) {isFirst = true;needToChange = true;}//如果第一个数就大于x,需要改变头节点
while(p!=NULL)
{
if(p->val < x)
{
if(isFirst)//需要改变头节点
{
ListNode* temp = p->next;
pre->next = p->next;
p->next=head;
lessTail = p;
head = p;
p = temp;
isFirst = false;
}
else
{
if(!needToChange)//前面部分都小于x,则不需要改变
{
lessTail = p;
pre = p;
p=p->next;
}
else//需要进行插入操作
{
ListNode* temp = p->next;
pre->next = p->next;
p->next = lessTail->next;
lessTail->next = p;
lessTail = p;//注意要改变小于x部分的尾节点
p = temp;
}
}
}
else
{
pre = p;
p=p->next;
needToChange = true;
}
}
return head;
}
};
后记:在这里祝大家端午节快乐,也给端午节还在坚持刷题的我自己点个赞。哈哈!
【一天一道LeetCode】#86. Partition List的更多相关文章
- [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
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 划分链表 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(分区链表) 解题思路和方法
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)
86. 分隔链表 86. Partition List 题目描述 给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前. 你应当保留两个分区中每个节点的 ...
- 【LeetCode】86. Partition List 解题报告(Python)
[LeetCode]86. Partition List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...
随机推荐
- 图解JavaScript原型和原型链
先看看最简单的栗子: //构造函数 function People(name, age){ this.name = name; this.age = age; } //原型对象(所有由构造函数实例而来 ...
- 安装Leanote极客范的云笔记
前言 在这个互联网知识呈爆炸增长的时代,作为一个程序员要掌握的知识越来越多,然再好的记性也不如烂笔头,有了笔记我们就是可以时常扒拉扒拉以前的知识,顺便可以整理下自己的知识体系. 如今市面上云笔记产品, ...
- Vue 波纹按钮组件
代码链接:https://github.com/zhangKunUserGit/vue-component 效果图: 大家可以在线运行: https://zhangkunusergit.github. ...
- Bootstrap3 排版-页面主体
Bootstrap 将全局 font-size 设置为 14px,line-height 设置为 1.428.这些属性直接赋予 元素和所有段落元素.另外,<p> (段落)元素还被设置了等于 ...
- GitLab服务器IP地址设置
最近使用GitLab 搭建了Git的私有仓库,但是发现私有仓库的地址居然是localhost,不是本机的IP地址,最后百度了一下,找了很久才找到,特此记录一下. 首先说明一下,我linux虚拟机的IP ...
- 【mybatis深度历险系列】mybatis中的动态sql
最近一直做项目,博文很长时间没有更新了,今天抽空,学习了一下mybatis,并且总结一下.在前面的博文中,小编主要简单的介绍了mybatis中的输入和输出映射,并且通过demo简单的介绍了输入映射和输 ...
- Ajax框架,DWR介绍,应用,例子
使用Ajax框架 1. 简化JavaScript的开发难度 2. 解决浏览器的兼容性问题 3. 简化开发流程 常用Ajax框架 Prototype 一个纯粹的JavaScript函数库,对Ajax提供 ...
- 自制Linux重命名命令
相比于Windows上的ren命名,Linux还真的是没有一个特定的重命名的命令.(虽然可以间接的使用mv来实现).下面我就来自己写一个简单的重命名命令. 准备工作 操作系统: Linux内核的系统都 ...
- 有n个数,输出其中所有和为s的k个数的组合。
分析:此题有两个坑,一是这里的n个数是任意给定的,不一定是:1,2,3...n,所以可能有重复的数(如果有重复的数怎么处理?):二是不要求你输出所有和为s的全部组合,而只要求输出和为s的k个数的组合. ...
- Spring之Core模块
Core模块主要的功能是实现了控制反转与依赖注入.Bean配置以及加载.Core模块中有Beans.BeanFactory.BeanDefinitions.ApplicationContext等概念 ...