【leetcode】Partition List
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
.
/**
* 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的更多相关文章
- 【LeetCode】Partition List ——链表排序问题
[题目] Given a linked list and a value x, partition it such that all nodes less than x come before nod ...
- 【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 ...
- 【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 ...
- 【LeetCode】86. Partition List 解题报告(Python)
[LeetCode]86. Partition List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...
- 【leetcode】698. Partition to K Equal Sum Subsets
题目如下: 解题思路:本题是[leetcode]473. Matchsticks to Square的姊妹篇,唯一的区别是[leetcode]473. Matchsticks to Square指定了 ...
- 【LeetCode】动态规划(下篇共39题)
[600] Non-negative Integers without Consecutive Ones [629] K Inverse Pairs Array [638] Shopping Offe ...
- 【LeetCode】双指针 two_pointers(共47题)
[3]Longest Substring Without Repeating Characters [11]Container With Most Water [15]3Sum (2019年2月26日 ...
- 【LeetCode】813. Largest Sum of Averages 解题报告(Python)
[LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
随机推荐
- redis缓存分页数据ID
1.用户通过分类.属性进来分页时 如果没有缓存,就读数据库前10页的数据Id,转为json,根据cate_分类1+cate_分类2+cate_分类3+arr_属性1+arr_属性2+arr_属性3作为 ...
- idea配置2个tomcat
复制tomcat 分别放在不同地方
- svn更改分支名字,move命令
名称 svn move — 移动一个文件或目录. 概要 svn move SRC DST 描述 这个命令移动文件或目录到你的工作拷贝或者是版本库. 提示 这个命令同svn copy加一个svn del ...
- JS刷新页面总和!多种JS刷新页面代码!
1)<meta http-equiv="refresh"content="10;url=跳转的页面">10表示间隔10秒刷新一次2)<scri ...
- Oracle CASE WHEN 用法介绍
1. CASE WHEN 表达式有两种形式 --简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ELSE '其他' END --Case搜索 ...
- socket编程报异常java.io.EOFException
一个客户端连接服务器的小程序,服务器端可以正常读取客户端发来的数据 但是当客户端关闭时,服务端也关闭了,并且抛出如下的异常: java.io.EOFException at java.io.DataI ...
- 快速切换IP的批处理!
内容如下: @echo off color 1A Title [SMART专用 IP设置V1.0] cls echo. echo SMART专用 IP设置V1.0 %date%%time% echo. ...
- IE打开报错,提示该内存不能为read的解决办法!
由于最近我遇到过一次浏览器打不开的情况,出错的错误提示为 浏览器错误:“0x5ddfddac”指令引用的“0x00000020”内存,该内存不能为read经过杀毒及IE修复均不能解决!(没试过360急 ...
- corntab
http://www.cnblogs.com/peida/archive/2013/01/08/2850483.html
- git/gitLab
gitlab安装:http://www.360doc.com/content/15/0603/14/21631240_475362133.shtml http://www.cnblogs.com/wi ...