【一天一道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:// ...
随机推荐
- java.sql.SQLException: **** [SQLServer]对象名 "XXXX"无效
原因:进到数据库里面,但是没有选择数据库. 方法:检查数据源配置,这玩意容易看出.难得是多数据库操作时,切换数据源!
- Jackson工具
Jackson Jackson包含一个core JAR,和两个依赖core JAR的JAR: jackson-core-2.2.3.jar(核心jar包,下载地址) jackson-annotatio ...
- Tomcat关闭日志输出
tomcat中禁用catalina.out的输出,又可能很大. 1.直接修改catalina.sh文件的输出语句. 在文件中找到以下内容. if [ -z "$CATALINA_OUT&qu ...
- 记录 Python3 安装 Scrapy 遇到的问题
开发环境:Windows 10 + Python 3 使用 pip 去安装 Scrapy, pip install scrapy , 报了一个错误. 原因:加 --user 的作用是显式指定安装在用 ...
- 剑指架构师系列-MySQL常用SQL语句
(1)分清HAVING与WHERE的区别: HAVING 子句使你能够指定过滤条件,从而控制查询结果中哪些组可以出现在最终结果里面.WHERE 子句对被选择的列施加条件,而 HAVING 子句则对 G ...
- 剑指架构师系列-持续集成之Maven+Nexus+Jenkins+git+Spring boot
1.Nexus与Maven 先说一下这个Maven是什么呢?大家都知道,Java社区发展的非常强大,封装各种功能的Jar包满天飞,那么如何才能方便的引入我们项目,为我所用呢?答案就是Maven,只需要 ...
- Spring Security基于Java配置
Maven依赖 <dependencies> <!-- ... other dependency elements ... --> <dependency> < ...
- Android桌面小插件——Widget
Android桌面小插件--Widget 效果图 实现 1. 创建Widget类 创建一个Widget类,并实现页面创建的时候,就实现显示时间 package com.kongqw.kqwwidget ...
- springMVC源码分析--异常处理机制HandlerExceptionResolver简单示例(一)
springMVC对Controller执行过程中出现的异常提供了统一的处理机制,其实这种处理机制也简单,只要抛出的异常在DispatcherServlet中都会进行捕获,这样就可以统一的对异常进行处 ...
- 不规则递归转换为while,留底
我发现当参数并不太多时,从性能的角度来看,没必要用一个class来保存参数(虽然看起来更加生动形象),直接用最简单的元组就可以了. from hanoi import * # example tree ...