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的更多相关文章
随机推荐
- 迭代导出word 文档
Map迭代的使用: Map map = new HashMap() ; Iterator it = map.entrySet().iterator() ; while (it.hasNext()) { ...
- 【Python爬虫基础】抓取知乎页面所有图片
抓取地址所有图片 #! /usr/bin/env python from urlparse import urlsplit from os.path import basename import ur ...
- 让 Dreamweaver 支持 Emmet(原ZenCoding)
注:目前暂不支持 DW CC,期待作者早是更新.Update:2013/10/12 鉴于某些原因,每个 Coder 所钟爱的 IDE 各不相同.而作为一个软件爱好者,我几乎所有 IDE 都使用过一段时 ...
- sql server 2008 在安装了活动目录以后无法启动服务了
软件环境: windows server 2008 r2 ms sql server 2008 r2 在安装活动目录以前,数据库是正常运行的. 安装了活动目录以后,数据库启动时就提示无法启动.出错的信 ...
- Android 开发 对话框Dialog dismiss和hide方法的区别
http://ningtukun.blog.163.com/blog/static/186541445201310151539697/ dismiss和hide方法都可以隐藏对话框,在需要的时候也可以 ...
- 华为OJ:2041 放苹果
这道题难点不在于代码怎么写,而是思路怎么想. 感觉一般这样的题要么你理好一个思路要么你最后总结出一个公式,要么你自己模拟它的运作方式,用迭代,或者递归的方式来做. 有点像我们曾经学的排列组合. 对于m ...
- Ignatius and the Princess III --undo
Ignatius and the Princess III Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (J ...
- 常用的JS数据类型转换方法
JS 数据类型转换的方法有以下3种:1)使用转换函数2)强制类型转换3)利用js变量弱类型特性进行转换 1:js提供了parseInt()和parseFloat()这两个转换函数. 这里输入内容par ...
- Sybase常用函数
==================================常用函数===========================================字符串函数1)ISNULL(EXP1, ...
- 自定义View-6 状态按钮 滑动 点击
View public class SwitchButton extends View implements OnClickListener, OnTouchListener { privat ...