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. 学习blus老师js(5)--DOM操作应用高级

    一.表格应用 - 1 获取 tBodies.tHead.tFoot.rows.cells 一个表格可以有很多tbody,所以tBodies是数组: 一个表格只能有一个thead和tfoot,所以tHe ...

  2. 马士兵Spring-声明式事务管理-XML

    1.com.cy.model中User.java  和 Log.java 实体 和上一节一样: 2.DAO的实现类com.cy.dao.impl中的UserDAOImpl.LogDAOImpl.jav ...

  3. while循环优化版本-for循环

    for i in range(0,10): if i <3: print("loop ",i) else : continue print("hehe...&quo ...

  4. icape3 的使用

    在FPGA中,有时需要使用用户代码重配置FPGA,配置的内容可以是flash或者是其他的来源这样FPGA的启动模式有关,在本实验中配置文件是存放在flash中.实际的操作步骤如下: 1:生成一个工程, ...

  5. poj-3253-Fence Repair(哈夫曼)

    /* Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19914 Accepted: 6314 Desc ...

  6. WordVBA常用项

    将光标所在段落选中 Selection.MoveUp unit:=wdParagraphSelection.MoveDown unit:=wdParagraph, Extend:=wdExtend ' ...

  7. JS判断不能为空实例代码

    JS判断不能为空实例代码.分享在此. 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" " ...

  8. leetcode190

    public class Solution { public uint reverseBits(uint n) { var list = new List<uint>();//逆序的二进制 ...

  9. c# 之 unsafe

    unsafe必须谨慎使用,而且很多时候是不需要unsafe的.通常只有在对性能要求高和与其它模块交互需要用到这个.比如指针操作,无边界检查的数组操作,与一些其他语言编写的模块交互等不安全代码 C#是可 ...

  10. 在coursera上有哪些值得推荐的课程

    来自知乎 https://www.zhihu.com/question/22436320/answer/224996328