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. 使用微信JS-SDK调用微信浏览器的接口

    先附上微信公众平台的相关链接: 微信公众平台:https://mp.weixin.qq.com/ 微信公众平台开发文档:https://mp.weixin.qq.com/wiki 微信公众平台JS-S ...

  2. 从HTML原型到jsp页面完美转型攻略(教你即使不会写代码也能弄出漂亮的网页)

    大家都知道软件项目(web)开发之前都要先做原型设计,而我们使用的比较多的一款原型设计软件就是Axure rp了.在Axure rp上画原型不需要任何编码能力,而且生成的原型可以在浏览器上运行.除了没 ...

  3. virtualbox 中的linux 共享文件 发生文件系统类型错误的解决办法

    转自:http://blog.csdn.net/ls1160/article/details/24913391 最近在研究linux下的安卓源代码编译,遇到了一些问题,在虚拟机的共享文件上. 因为联网 ...

  4. 从头开始写框架(一):浅谈JS模块化发展

    博客申请下来已经过去一个月了,一直不知道写点什么,毕竟我的文笔不是很好orz. 不过既然申请下来了,不写点什么总是觉得很可惜.正好最近在自己写框架,就把自己的进程和一些心得体会分享出来吧. 写在前面: ...

  5. Web前端开发规范文档(google规范)

    (Xee:其实没什么规范约束,但是养成一种好习惯,何乐而不为?) 区分大小写 xhtml  区分大小写,xhtml要求 标签名 属性名 值都要小写,并且要有双引号和 标签闭合. css 元素名称以及i ...

  6. [译]Introducing ASP.NET 5

    原文:http://weblogs.asp.net/scottgu/introducing-asp-net-5 ASP.NET 5预览版发布了, 可以在这下载最新的Visual Studio 2015 ...

  7. HTML Table导出为Excel的方法

    HTML Table导出为Excel的方法: 直接上源码 <html> <head> <meta http-equiv="Content-Type" ...

  8. [原创]svn 常见错误总结

    错误: Unable to make name in 'X:\nfs\drivers\can_uart\.svn\tmp' 解决: 改变当前文件夹的权限 linux 下显示修改的文件名 参考链接:ht ...

  9. centos 安装mysql

    wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm rpm -ivh mysql-community-rele ...

  10. mongodb python image 图像存储读取

    最近做一些数据库调研的工作,目标是实现影像更快的入库.出库.查询,并实现并行访问等操作. 将结果总结成一个mongoImg类,也算是小结吧. ''' Created on 2013-8-6 class ...