Partition List

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大的节点,并保存下来
然后把他们两连起来
 

 /**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *partition(ListNode *head, int x) { ListNode *p=head;
ListNode *p0=NULL;
ListNode *head1=NULL;
ListNode *head2=NULL;
while(p!=NULL)
{
ListNode *tmp;
if(p->val<x)
{
tmp=new ListNode(p->val);
if(p0==NULL) head1=tmp;
else p0->next=tmp;
p0=tmp;
} p=p->next;
} ListNode *head1End=p0;
p=head;
p0=NULL;
while(p!=NULL)
{
ListNode *tmp;
if(p->val>=x)
{
tmp=new ListNode(p->val);
if(p0==NULL) head2=tmp;
else p0->next=tmp;
p0=tmp;
}
p=p->next;
} if(head1End!=NULL) head1End->next=head2;
else head1=head2; return head1;
}
};
第二种思路,与第一种方法类似,只是不新建链表进行存储
 
 /**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *partition(ListNode *head, int x) { if(head==NULL) return NULL; ListNode *p=head; ListNode *head1=NULL;
ListNode *head2=NULL; ListNode *p0=NULL;
ListNode *p1=NULL; while(p!=NULL)
{
if(p->val<x)
{
if(p0==NULL) head1=p;
else p0->next=p;
p0=p;
}
else
{
if(p1==NULL) head2=p;
else p1->next=p;
p1=p;
} p=p->next;
} if(p1!=NULL) p1->next=NULL;
if(p0!=NULL) p0->next=head2;
else head1=head2; return head1;
}
};

【leetcode】Partition List的更多相关文章

  1. 【LeetCode】Partition List ——链表排序问题

    [题目] Given a linked list and a value x, partition it such that all nodes less than x come before nod ...

  2. 【leetcode】Partition List(middle)

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  3. 【Leetcode】Partition List (Swap)

    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 解题报告(Python)

    [LeetCode]86. Partition List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...

  5. 【leetcode】698. Partition to K Equal Sum Subsets

    题目如下: 解题思路:本题是[leetcode]473. Matchsticks to Square的姊妹篇,唯一的区别是[leetcode]473. Matchsticks to Square指定了 ...

  6. 【LeetCode】动态规划(下篇共39题)

    [600] Non-negative Integers without Consecutive Ones [629] K Inverse Pairs Array [638] Shopping Offe ...

  7. 【LeetCode】双指针 two_pointers(共47题)

    [3]Longest Substring Without Repeating Characters [11]Container With Most Water [15]3Sum (2019年2月26日 ...

  8. 【LeetCode】813. Largest Sum of Averages 解题报告(Python)

    [LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  9. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

随机推荐

  1. HashSet与HashMap的区别

    本文由 ImportNew - 唐小娟 翻译自 Javarevisited.欢迎加入翻译小组.转载请见文末要求. HashMap和HashSet的区别是Java面试中最常被问到的问题.如果没有涉及到C ...

  2. C# 连接mongodb副本集+分片读写分离及学习资料

    一.副本集配置 搭建完毕,1台主实例.1台从实例.1台仲裁实例.mongodb建议副本集中的机器数量为奇数,即至少需要3台实例 二.副本集连接字符串 1.读 mongodb://secondary.c ...

  3. 是时候放弃Uploadify了

    1.Uploadify是什么? Uploadify是来自国外的一款优秀jQuery插件,主要功能是批量上传文件. 这话是复制百度百科的,是:基于jQuery开发的文件上传插件. 2.为什么大家都在用? ...

  4. Linux jdk1.7安装与 jdk1.6卸载

    昨天安装zookeeper时需要java环境,也就是安装jdk    安装完jdk1.7后,配置好环境变量, vim ~/.bashrc       JAVA_HOME=安装路径 export PAT ...

  5. c# 中使用memcached

    1.首先下载memcached 服务端 2.使用Enyim.Caching .Net 客户端 3.配置web.config    <sectionGroup name="QuickBo ...

  6. html css 网络 页面布局 颜色 参考 拾取器网址

    http://blog.163.com/wujinhongisme@126/blog/static/3613698020095115919389/ ========================== ...

  7. oracle 中的trunc()函数及加一个月,一天,一小时,一分钟,一秒钟方法

    返回处理后的数据,不同于round()(对数值进行四舍五入处理),该函数不对指定小数前或后的数值部分进行舍入处理. 语法:trunc(number[,decimals]) 其中,number为待做处理 ...

  8. 2.交通聚类 -层次聚类(agnes)Java实现

    1.项目背景 在做交通路线分析的时候,客户需要找出车辆的行车规律,我们将车辆每天的行车路线当做一个数据样本,总共有365天或是更多,从这些数据中通过聚类来获得行车路线规律统计分析. 我首先想到是K-m ...

  9. Support for Xpm library: no问题

    编译gd,出现问题 Support for Xpm library:          no 怎么解决? 解决办法就是去下载libXpm库安装: yum -y install libXpm-dev 这 ...

  10. Android 网络请求框架android-async-http问题

    今天通过接口请求服务器的一些app数据,发现一个很奇怪的问题,请求一个链接的时候,通常在第一次请求发起的时候没有什么问题,能很快的拿到数据,但是 往后再去请求的时候就会等待很久,而且最后会请求失败,一 ...