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. Java renameTo()重新命名此抽象路径名表示的文件

    Java手册 renameTo public boolean renameTo(File dest) 重新命名此抽象路径名表示的文件. 此方法行为的许多方面都是与平台有关的:重命名操作无法将一个文件从 ...

  2. 关于ROS证书导入的步骤

    在群里的vibbow大神指点下,做了一个ROS证书导入的步骤 1.到阿里云申请的免费证书清单如下:(如果你准备的自签名证书,那么在客户端也需要安装证书才行,否则就要到网上去申请真实的,或者花钱买的证书 ...

  3. python mysql模块

    多次使用python操作mysql数据库,先与大家分享一下,关于如何使用python操作mysql数据库.mysql并不是python自带的模块,因此需要下载安装.(在windows平台下介绍该使用过 ...

  4. 转 linux 下装 usb driver

    http://www.george-smart.co.uk/wiki/Xilinx_JTAG_Linux

  5. 使用打印方法时,要先引用命名空间: Using System.Drawing.Pringing

    使用打印方法时,要先引用命名空间: Using System.Drawing.Pringing PrintDocument类的重要属性和方法:属性:DocumentName  设置打印文档时要显示的文 ...

  6. Redis 哨兵模式 带密码单机

    语法 https://segmentfault.com/a/1190000002680804 启动3台redis 6379,6380,6381 cp 多个redis.conf文件 开启daemoniz ...

  7. Filter的连接过程《转》

    原文地址:https://yq.aliyun.com/articles/48113 7.Filter的连接过程 DirectShow提供了多方法来连接Filter,如IFilterGraph::Con ...

  8. UNITY 打APK是如何确定哪些资源有用哪些无用的

    一切从build settings开始,它即是 构建列表,构建运行包当然从它开始. 1,只有在构建列表中的场景和场景引用资源才会被打进包里,其它资源除了2,3位置都不会被打包 2,streamming ...

  9. 让 IE6支持max-height

    min-height min-height:100px; _height:100px max-height max-height:200px; overflow:auto;/*超出部分显示滚动条*/ ...

  10. [C++] STL相关面试题

    (1) 为何map和set的插入删除效率比用其他序列容器高? 因为map和set的内部数据结构是红黑树,它的插入和删除不需做内存的拷贝和移动.(红黑树的插入和删除是log(n)的). (2) 为何每次 ...