【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 ...
随机推荐
- tomcat的文件路径 servelet的配置 以及maven中的WEB-INF的路径
Tomcat JavaWeb应用的组成结构 开发JavaWeb应用时,不同类型的文件有严格的存放规则,否则不仅可能会使web应用无法访问,还会导致web服务器启动报错 WebRoot →Web应用所在 ...
- heartbleed漏洞利用
1. heartbleed漏洞扫描: 2. heartbleed漏洞利用: poc.py 117.52.93.111 貌似没有打到管理员账号密码,可能是管理员没登录,其实,可以写一个自动 ...
- OC-苹果官方文档
苹果官方文档 help>documentation>左边iOS>language>objective>the objective-c programming langua ...
- struts.xml框架
1.首先在.jsp文件中<form action="/项目名称/login" method="post"> 2.然后浏览器会访问struts.xml ...
- apache 的工作模式
总结:访问量大的时候使用 worker模式: 每个进程,启动多个线程来处理请求,每个线程处理一次请求,对内存要求比较高. prefoek模式 : 每个子进程只有一个线程,一次请求一个进程. 什么是a ...
- JBoss7 安装配置
一.下载安装 1.下载地址: http://www.jboss.org/jbossas/downloads ,下载Certified Java EE 6 Full Profile版本. 2.解压 jb ...
- highstock 的tooltip框里面的内容 保留两位小数的办法
$("#flux_chart_container").highcharts(' }, borderWidt ...
- [译]A Beginner’s Guide to npm — the Node Package Manager
原文: http://www.sitepoint.com/beginners-guide-node-package-manager/ Installing Node.js 验证你的安装是否成功. $ ...
- protect和private 的区别
protect和private 的区别 public 表示全局,类内部外部子类都可以访问: private表示私有的,只有本类内部可以使用: protected表示受保护的,只有本类或子类或父类中可以 ...
- html 图像映射
个人先做而一个例子 <body> <img src="图像映射/enterdesk.com-D69055E2B422567CB273963EA05FF7D4.jpg&quo ...