LeetCode_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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(head == NULL) return head;
ListNode *pre , *firstLarger, * preCur, * cur;
pre = NULL; firstLarger = head;
while(firstLarger && firstLarger->val < x){
pre = firstLarger;
firstLarger = firstLarger ->next;
}
if(firstLarger == NULL) return head; preCur = firstLarger;
cur = preCur->next; while(cur != NULL){
if( cur->val >= x){
preCur = cur;
cur = cur->next;
continue;
} preCur->next = cur->next;
cur->next = firstLarger;
if(pre == NULL){
pre = cur;
head = cur;
}else{
pre->next = cur;
pre = cur;
}
cur = preCur ->next;
} return head;
}
};
LeetCode_Partition List的更多相关文章
随机推荐
- 删除WIN7系统的共享文件
运行里面输入fsmgmt.msc命令,点开共享目录,选定要取消共享的文件右键,停止共享.
- 多个ajax按照顺序执行的方法
$.ajax({ dataType: "json", async: false, //只需将此属性设置为false url: ~~, type: "GET", ...
- HDOJ 1323 Perfection(简单题)
Problem Description From the article Number Theory in the 1994 Microsoft Encarta: "If a, b, c a ...
- 2016年如何选择 Linux 发行版
不管是在企业级应用还是在消费者领域,2015 对于 Linux 来说都是极其重要的一年.作为一个从 2005 年就开始使用 Linux 的老用户,我有幸见证了 Linux 过去这 10 年里的重大发展 ...
- Hive和HBase的区别
一.两者分别是什么: Apache Hive是一个构建在Hadoop基础设施之上的数据仓库.通过Hive可以使用HQL语言查询存放在HDFS上的数据.HQL是一种类SQL语言,这种语言最终被转 ...
- html(三)
今天自己画了个安卓机器人,之前听徐大大讲过一次,查手册去动手的时候其实发觉不是很难,这种规则的图像还是很好画的,主要是用<div>标签和<span>标签去做的,通过CSS添加样 ...
- java-下载excel
在java程序里面处理excel,我觉得比较方便的方式是先做出一个excel的模板(比如定义表头信息.表格名称等),然后根据这个模板往里面填充数据 我这里演示的是使用poi处理2007以上版本的exc ...
- android 多项对话框
在main.xml中 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:a ...
- [Angular 2]ng-class and Encapsulated Component Style2
Many Components require different styles based on a set of conditions. Angular 2 helps you style you ...
- sed删除空行和注释行
最近在看前辈们写的代码,他们把没有用的代码是注释掉而不是删掉.没用的代码和注释很乱,看着心烦,就把注释删掉来解读,顿时爽快多了. 不多说了,直接举例子 比如一个文本文件 data 里的内弄为 cat ...