[LeetCode 题解]: Partition 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.
题意:
给定一个链表以及一个数x。
要求:
将队列中所有小于x的节点放到大于或等于x的节点之前。
思路:
根据题意,很容易想到利用两个链表:
lower按照先后顺序存放小于x的节点;
higher存放大于等于x的节点。
最后合并分割后的链表,按照lower在前,higher在后的顺序。
class Solution {
public:
ListNode *partition(ListNode *head, int x) {
if(head==NULL || head->next==NULL) return head;
// divide list into two parts:
// lower part with all nodes whose value is less than x
// higher part with all nodes whose value is more than x
ListNode *lower = new ListNode(-);
ListNode *higher = new ListNode(-);
ListNode *ltail = lower;
ListNode *htail = higher;
ListNode *tmp = head;
while(tmp){
if(tmp->val < x){
ltail->next = tmp;
ltail = ltail->next;
}else{
htail->next = tmp;
htail = htail->next;
}
tmp = tmp->next;
}
htail->next=NULL; // set end of a list
ltail->next=higher->next; // append higher part to the tail of lower part
return lower->next;
}
};
[LeetCode 题解]: Partition List的更多相关文章
- LeetCode: Palindrome Partition
LeetCode: Palindrome Partition Given a string s, partition s such that every substring of the partit ...
- 【LeetCode题解】二叉树的遍历
我准备开始一个新系列[LeetCode题解],用来记录刷LeetCode题,顺便复习一下数据结构与算法. 1. 二叉树 二叉树(binary tree)是一种极为普遍的数据结构,树的每一个节点最多只有 ...
- leetcode题解-122买卖股票的最佳时期
题目 leetcode题解-122.买卖股票的最佳时机:https://www.yanbinghu.com/2019/03/14/30893.html 题目详情 给定一个数组,它的第 i 个元素是一支 ...
- 【LeetCode题解】3_无重复字符的最长子串(Longest-Substring-Without-Repeating-Characters)
目录 描述 解法一:暴力枚举法(Time Limit Exceeded) 思路 Java 实现 Python 实现 复杂度分析 解法二:滑动窗口(双指针) 思路 Java 实现 Python 实现 复 ...
- 【LeetCode题解】225_用队列实现栈(Implement-Stack-using-Queues)
目录 描述 解法一:双队列,入快出慢 思路 入栈(push) 出栈(pop) 查看栈顶元素(peek) 是否为空(empty) Java 实现 Python 实现 解法二:双队列,入慢出快 思路 入栈 ...
- 【LeetCode题解】232_用栈实现队列(Implement-Queue-using-Stacks)
目录 描述 解法一:在一个栈中维持所有元素的出队顺序 思路 入队(push) 出队(pop) 查看队首(peek) 是否为空(empty) Java 实现 Python 实现 解法二:一个栈入,一个栈 ...
- 【LeetCode题解】844_比较含退格的字符串(Backspace-String-Compare)
目录 描述 解法一:字符串比较 思路 Java 实现 Python 实现 复杂度分析 解法二:双指针(推荐) 思路 Java 实现 Python 实现 复杂度分析 更多 LeetCode 题解笔记可以 ...
- 【LeetCode题解】25_k个一组翻转链表(Reverse-Nodes-in-k-Group)
目录 描述 解法一:迭代 思路 Java 实现 Python 实现 复杂度分析 解法二:递归(不满足空间复杂度) 思路 Java 实现 Python 实现 复杂度分析 更多 LeetCode 题解笔记 ...
- 【LeetCode题解】24_两两交换链表中的节点(Swap-Nodes-in-Pairs)
目录 描述 解法一:迭代 思路 Java 实现 Python 实现 复杂度分析 解法二:递归(不满足空间复杂度要求) 思路 Java 实现 Python 实现 复杂度分析 更多 LeetCode 题解 ...
随机推荐
- 【并发编程】Executor架构介绍
要点总结 Executor表示的任务类型 主要有3种: Runnable: 无返回值,无异常抛出: Callable:有返回值,可以异常抛出: Future任务: 表示异步计算,可取消: 通过newT ...
- sorl 6.6.0 定时更新索引
solr 定时更新索引 – solr 6.6.0 – dataimport.scheduler 这里先重点说下,定时更新引用的org.apache.solr.handler.dataimport.sc ...
- js 各种距离
网页可见区域宽 document.body.clientWidth 网页可见区域高 document.body.clientHeight 网页可见区域宽(包括边线的宽) document.b ...
- bootstrap更新数据层
mq推送数据,表格实时更新,发现销毁表格不太合适,整体表格闪动,于是选择更新数据层. 先初始化表格,然后在推送数据的时候先循环遍历数据 例如: initDevTable(data.operatingL ...
- Rhythmk 学习 Hibernate 01 - maven 创建Hibernate 项目之 增删改查入门
1.环境: Maven :3.1.1 开发工具:Spring Tool Suite 数据库 : Mysql 5.6 2.项目文件结构 文件代码: 2.1 .pom.xml <project x ...
- 13 并发编程-(线程)-异步调用与回调机制&进程池线程池小练习
#提交任务的两种方式 #1.同步调用:提交完任务后,就在原地等待任务执行完毕,拿到结果,再执行下一行代码,导致程序是串行执行 一.提交任务的两种方式 1.同步调用:提交任务后,就在原地等待任务完毕,拿 ...
- 64. Minimum Path Sum (Graph; DP)
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- 通过http流发送post请求
一般都是用curl扩展来完成,看了手册的通过stream的方式更加简单. 请求脚本stream.php $url = 'http://localhost/stream_api.php'; $body ...
- lombok 介绍及基本使用方法
Lomboz是一个基于LGPL的开源J2EE综合开发环境的Eclipse插件,对编码,发布,测试,以及debug等各个软件开发的生命周期提供支持,支持JSP,EJB等.Lomboz是Eclipse的一 ...
- MySqlDBHelper
代码: using System; using System.Collections.Generic; using System.Linq; using System.Web; using Syste ...