【一天一道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:// ...
随机推荐
- js获取当前的日期时间 格式“yyyy-MM-dd HH:MM:SS”
function getNowFormatDate() { var date = new Date(); var seperator1 = "-"; var seperator2 ...
- 【集合框架】JDK1.8源码分析之HashMap(一) 转载
[集合框架]JDK1.8源码分析之HashMap(一) 一.前言 在分析jdk1.8后的HashMap源码时,发现网上好多分析都是基于之前的jdk,而Java8的HashMap对之前做了较大的优化 ...
- Java并发中的CopyOnWrite容器
Copy-On-Write简称COW,是一种用于程序设计中的优化策略.其基本思路是,从一开始大家都在共享同一个内容,当某个人想要修改这个内容的时候,才会真正把内容Copy出去形成一个新的内容然后再改, ...
- 基于PHP的地址清洗调用案例-快宝开放平台
快宝地址清洗,纠正错误地址.识别不完整地址.地址补全,并输出结构化地址数据的通用解决方案.广泛应用于快递行业,电商行业,ERP应用等. 快宝开放平台-地址清洗对接API:http://open.kua ...
- 360搜索引擎so自动收录php改写方案——适合phpcms等cms
360搜索引擎自动收录功能,官方提供了代码,带式,十分坑爹,没有提供批量提交入口,只是提供了一段js代码,关键是 一个js去下载另外一个js,document.write到文档,然后再 重复2遍如此工 ...
- VUE相关资料合集
===官方=== https://github.com/vuejs/vue vue-components组件库 ---PC端--- https://github.com/ElemeFE/element ...
- css 中calc无效属性值问题
width:calc(50%-20px); 这样书写是无效的因为calc中计算的两个因子同运算符号之间必须存在空格:
- MongoDB 监控
在你已经安装部署并允许MongoDB服务后,你必须要了解MongoDB的运行情况,并查看MongoDB的性能.这样在大流量得情况下可以很好的应对并保证MongoDB正常运作. MongoDB中提供了m ...
- 实体类双向映射进行Json序列化时出现无限循环的解决问题
1.@JsonIgnoreProperties 指定的字段不会被序列化,如下则ExamPaper的directory字段不会被序列化 @OneToMany(mappedBy = "direc ...
- Java语言程序设计-Markdown格式作业模板
Markdown格式作业模板如下,大家可以直接复制粘贴使用. 注意:作业中不能只写答案,题目本身也要出现.. # 1. 本章学习总结 你对于本章知识的学习总结 # 2. 书面作业 **Q1 java ...