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. APP图标设计小技巧:在iOS上快速获得APP图标的真实预览图

    严格来说,这并不是一篇关于前端开发的文章,因为涉及到的知识非常浅.这只是一个向设计狮们分享的小经验,只是其中用到了一些前端内容. 最近接了个私活,了解到一个初创公司正在高价悬赏Logo(主要用于APP ...

  2. HDFS只支持文件append操作, 而依赖HDFS的HBase如何完成数据的增删改查

    转:http://www.th7.cn/db/nosql/201510/135382.shtml 1. HDFS的文件append功能 早期版本的HDFS不支持任何的文件更新操作,一旦一个文件创建.写 ...

  3. OpenCV KNN 之 使用方法

    http://blog.csdn.net/WL2002200/article/details/43149229 OpenCV 中KNN构造函数如下. C++: CvKNearest::CvKNeare ...

  4. 编写和运行简单的"Hello World"操作系统内核

    通常编写一个操作系统内核是一项浩大的工程.但我今天的目标是制作一个简单的内核,用比较方便的方法在虚拟机上验证它能够被grub装载和运行,并且可通过gdb进行调试,为接下去的工作创造一个基础环境. 首先 ...

  5. 高可用Mysql架构_Mycat集群部署(HAProxy + 两台Mycat+Mysql双主双从)

    既然大家都知道了Mysql分布式在大型网站架构中的作用,在这里就不再阐述.本片博客文章是基于我曾经搭建过的一个Mysql集群基础上实现的,实现过双主热备.读写分离.分库分表. 博客链接:http:// ...

  6. 20145321 《Java程序设计》第9周学习总结

    20145321 <Java程序设计>第9周学习总结 教材学习内容总结 第十六章 整合数据库 16.1 JDBC 1.JDBC简介: JDBC是Java联机数据库的标准规范,它定义一组标准 ...

  7. 33c3-pwn500-recurse

    Recurse 好记性不如烂笔头.当时没有记录,现在趁着有时间简单写一写,为以后留备份. 这个题目当时并没有队伍做出来,赛后作者发布了题目的源码和解答.看了之后发现是一个UAF漏洞,不过漏洞很不好找. ...

  8. IDEA 安装mybatis 插件 可以通过mapper定位到xml

    在使用IDEA的时候 ,通过mapper类定位到 xml文件是让人很头疼的问题! 无意中发现了这个插件!可以通过类方法直接定位到xml中!比较强大!哈哈! 这玩意好像是付费的! 不过不破解也能用! 哈 ...

  9. React Native区分安卓/iOS平台

    import { Platform, } from 'react-native'; alert(JSON.stringify(Platform)): android手机弹出:{"OS&quo ...

  10. LIS学习笔记(两种算法)O(n^2) 和 O(nlogn)

    2017-09-02 10:34:21 writer:pprp 最长上升子序列,具体分析看代码:O(n^2)的做法,dp的思想 分析:每次读一个进行扫描,如果当前读入的这个要比之前的大, 说明有可能加 ...