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.

将链表分割成两部分,大于某个数字的在左侧,小于等于某个数字的在右侧,用的方法比较蠢可能就是遍历一次分成两个链表,然后再将它们接起来。具体代码如下所示:

 class Solution {
public:
ListNode* partition(ListNode* head, int x) {
ListNode * helper1 = new ListNode(INT_MIN);
ListNode * helper2 = new ListNode(INT_MIN);
ListNode * p1 = helper1;
ListNode * p2 = helper2;
while(head){
if(head->val < x){
p1->next = head;
head = head->next;
p1 = p1->next;
p1->next = NULL;
}else{
p2->next = head;
head = head->next;
p2 = p2->next;
p2->next = NULL;
}
}
p1->next = helper2->next;
return helper1->next;
}
};

LeetCode OJ:Partition List(分割链表)的更多相关文章

  1. [LeetCode] 763. Partition Labels 分割标签

    A string S of lowercase letters is given. We want to partition this string into as many parts as pos ...

  2. [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 ...

  3. 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 ...

  4. [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 ...

  5. [LeetCode]86. Partition List分离链表

    /* 这个题是medium的意思应该是用双指针的方法做,如果使用下边的新建链表的方法,就是easy的题目了 双指针会用到很多链表的相连操作 */ public ListNode partition(L ...

  6. 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 ...

  7. LeetCode:分割链表【86】

    LeetCode:分割链表[86] 题目描述 给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前. 你应当保留两个分区中每个节点的初始相对位置. 示例 ...

  8. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

  9. [CareerCup] 2.4 Partition List 划分链表

    2.4 Write code to partition a linked list around a value x, such that all nodes less than x come bef ...

  10. LeetCode: Palindrome Partition

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

随机推荐

  1. NC开发笔记指导

    修改端口 InvocationInfoProxy.getInstance().get().getUserCode(); 前台 Nchome F:\JAVA\Projects\ERPEHROA\ufid ...

  2. JavaScript&jQuery获取url参数方法

    JavaScript&jQuery获取url参数方法 function getUrlParam(name){ var reg = new RegExp("(^|&)" ...

  3. 20145321 《Java程序设计》课程总结

    20145321 <Java程序设计>课程总结 读书笔记链接汇总 第一周读书笔记 第二周读书笔记 第三周读书笔记 第四周读书笔记 第五周读书笔记 第六周读书笔记 第七周读书笔记 第八周读书 ...

  4. 20145324 Java实验四

    在IDEA上操作 由于不会创建安卓模拟器失败 选择老师给的插件 成功 实验总结 开始开发安卓,感觉更难了,这次实验完全是看运气拼电脑的实验! 步骤 耗时 百分比 需求分析 10m 17% 设计 20m ...

  5. mongodb入门很简单(2)

    mongodb的安装 1.下载mongodb: www.mongodb.org  下载最新的stable版:我下载的版本是3.2.5 2.解压文件 3.不用编译:本身就是编译后的二进制可执行文件 打开 ...

  6. ssh-keygen 的 详解

    为了让两个Linux机器之间使用ssh不需要用户名和密码.所以采用了数字签名RSA或者DSA来完成这个操作. 模型分析 假设 A (192.168.20.59)为客户机器,B(192.168.20.6 ...

  7. linux安装coreseek

    coreseek就是一个中文词库加上sphinx组合而成的. 1.下载coreseek 下载到/usr/local/src目录文件下 wget  http://www.coreseek.cn/uplo ...

  8. Nodejs Q promise设计思路

    Nodejs Q promise库 前言 Q库为nodejs提供了一个基于promise的编程方式,从此避免了一层又一层的callback调用.不过Q的灵活性也给我造成了很大困扰,我可以用promis ...

  9. PyTorch源码解读之torchvision.transforms(转)

    原文地址:https://blog.csdn.net/u014380165/article/details/79167753 版权声明:本文为博主原创文章,未经博主允许不得转载. https://bl ...

  10. hiho#1080 更为复杂的买卖房屋姿势 线段树+区间更新

    #1080 : 更为复杂的买卖房屋姿势 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho都是游戏迷,“模拟都市”是他们非常喜欢的一个游戏,在这个游戏里面他们 ...