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的更多相关文章

  1. LeetCode: Palindrome Partition

    LeetCode: Palindrome Partition Given a string s, partition s such that every substring of the partit ...

  2. 【LeetCode题解】二叉树的遍历

    我准备开始一个新系列[LeetCode题解],用来记录刷LeetCode题,顺便复习一下数据结构与算法. 1. 二叉树 二叉树(binary tree)是一种极为普遍的数据结构,树的每一个节点最多只有 ...

  3. leetcode题解-122买卖股票的最佳时期

    题目 leetcode题解-122.买卖股票的最佳时机:https://www.yanbinghu.com/2019/03/14/30893.html 题目详情 给定一个数组,它的第 i 个元素是一支 ...

  4. 【LeetCode题解】3_无重复字符的最长子串(Longest-Substring-Without-Repeating-Characters)

    目录 描述 解法一:暴力枚举法(Time Limit Exceeded) 思路 Java 实现 Python 实现 复杂度分析 解法二:滑动窗口(双指针) 思路 Java 实现 Python 实现 复 ...

  5. 【LeetCode题解】225_用队列实现栈(Implement-Stack-using-Queues)

    目录 描述 解法一:双队列,入快出慢 思路 入栈(push) 出栈(pop) 查看栈顶元素(peek) 是否为空(empty) Java 实现 Python 实现 解法二:双队列,入慢出快 思路 入栈 ...

  6. 【LeetCode题解】232_用栈实现队列(Implement-Queue-using-Stacks)

    目录 描述 解法一:在一个栈中维持所有元素的出队顺序 思路 入队(push) 出队(pop) 查看队首(peek) 是否为空(empty) Java 实现 Python 实现 解法二:一个栈入,一个栈 ...

  7. 【LeetCode题解】844_比较含退格的字符串(Backspace-String-Compare)

    目录 描述 解法一:字符串比较 思路 Java 实现 Python 实现 复杂度分析 解法二:双指针(推荐) 思路 Java 实现 Python 实现 复杂度分析 更多 LeetCode 题解笔记可以 ...

  8. 【LeetCode题解】25_k个一组翻转链表(Reverse-Nodes-in-k-Group)

    目录 描述 解法一:迭代 思路 Java 实现 Python 实现 复杂度分析 解法二:递归(不满足空间复杂度) 思路 Java 实现 Python 实现 复杂度分析 更多 LeetCode 题解笔记 ...

  9. 【LeetCode题解】24_两两交换链表中的节点(Swap-Nodes-in-Pairs)

    目录 描述 解法一:迭代 思路 Java 实现 Python 实现 复杂度分析 解法二:递归(不满足空间复杂度要求) 思路 Java 实现 Python 实现 复杂度分析 更多 LeetCode 题解 ...

随机推荐

  1. 031:Cetus sharding

    目录 一.主机环境 二.搭建环境 1.准备环境 三.Cetus安装 1.下载包和安装依赖包 2.Cetus安装 1.安装说明 2.安装实施 四.Cetus配置 1.创建和修改配置文件 2.启动cetu ...

  2. How to create a site with AJAX enabled in MVC framework.

    How to create a site with AJAX enabled in MVC framework. The Project illustrates how to create a web ...

  3. mybatis相对于ibatis的优势

    2010年,apache的Ibatis框架停止更新,并移交给了google团队,同时更名为MyBatis.从2010年后Ibatis在没更新过,彻底变成了一个孤儿框架.一个没人维护的框架注定被myba ...

  4. iOS开发者有价值的工具集

    转载于:http://www.cocoachina.com/applenews/devnews/2014/0307/7936.html 我一直比较推崇聪明地工作要远胜于刻苦地工作.使用正确的工具可以帮 ...

  5. mybatis使用foreach进行批量插入和删除操作

    一.批量插入 1.mapper层 int insertBatchRoleUser(@Param("lists") List<RoleUser> lists);//@Pa ...

  6. Golang学习系列:(一)介绍和安装

    Golang学习系列:(一)介绍和安装 Java程序员带你来到Go的世界,让我们开始探索吧! Go是一种新的语言,一种并发的,带有垃圾回收的.快速编译的语言,它具有一下特点: 他可以在一台计算机上用几 ...

  7. How to Restart Qt Application

    How to restart QtApplication As we know, Restarting Application means to exit current application, t ...

  8. loadrunner12-用Chrome如何录制脚本

    1.下载Chrome最新版本: 2.打开需要录制的网页,按下F12按钮: 3.在弹出框中,选择Network标签,在该标签下单击红色按钮(开始录制按钮): 4.按下F5刷新页面(不管是否需要录制当前页 ...

  9. 自学PHP的野方法

    1.基础知识 最早的基础知识仅限于那么一点点的html和css,比牛毛还牛毛的一点点.所以最开始是从immoc上看视频和跟着练习,花了有一个多月,看完一个路径从:零开始学习ThinkPHP框架,由于基 ...

  10. wmi收集系统信息 发送到服务器打印

    #include "WMIManager.h" #include <vector> #include <string> #include <boost ...