46. 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
.
分析: 保持两个表头, 一个存大于等于 x 的结点,一个存小于 x 的结点。
注意:记住两个表尾,中间不能将其 next 指针设置为空。最后将存小值的表尾链接在另一个前面,两一个表尾 next 置为 NULL.
/**
* 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 *head1, *head2, *tail1, *tail2;
head1 = head2 = tail1 = tail2 = NULL;
while(head) {
if(head->val >= x) {
if(head2 == NULL) head2 = tail2 = head;
else { tail2->next = head; tail2 = tail2->next; }
} else {
if(head1 == NULL) head1 = tail1 = head;
else { tail1->next = head; tail1 = tail1->next; }
}
head = head->next;
}
if(head2) tail2->next = NULL;
if(head1) tail1->next = head2;
else head1 = head2;
return head1;
}
};
46. Partition List的更多相关文章
- LeedCde 题解目录
1. Longest Palindromic Substring ( 最长回文子串 ) 2. Median of Two Sorted Arrays (两个排序数组的中位数) 3. Sqrt(x) 4 ...
- ORACLE分区--表分区
.love_flying_snow Oracle表分区 Oracle . 废话少说,直接讲分区语法. Oracle表分区分为四种:范围分区,散列分区,列表分区和复合分区. 一:范围分区 就是根据数据库 ...
- 转:Oracle表分区
Oracle表分区分为四种:范围分区,散列分区,列表分区和复合分区. 一:范围分区 就是根据数据库表中某一字段的值的范围来划分分区,例如: 1. create table graderecord 2. ...
- Oracle表分区分为四种:范围分区,散列分区,列表分区和复合分区(转载)
一:范围分区 就是根据数据库表中某一字段的值的范围来划分分区,例如: 1 create table graderecord 2 ( 3 sno varchar2(10), 4 sname varcha ...
- 每天一个linux命令(46):vmstat命令
vmstat是Virtual Meomory Statistics(虚拟内存统计)的缩写,可对操作系统的虚拟内存.进程.CPU活动进行监控.他是对系统的整体情况进行统计,不足之处是无法对某个进程进行深 ...
- PLSQL_性能优化系列09_Oracle Partition Table数据分区表
2014-08-22 Created By BaoXinjian
- geeksforgeeks@ Minimum sum partition (Dynamic Programming)
http://www.practice.geeksforgeeks.org/problem-page.php?pid=166 Minimum sum partition Given an array, ...
- PAT1113: Integer Set Partition
1113. Integer Set Partition (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- A1113. Integer Set Partition
Given a set of N (> 1) positive integers, you are supposed to partition them into two disjoint se ...
随机推荐
- 1362. Classmates 2
http://acm.timus.ru/problem.aspx?space=1&num=1362 水题,树形DP 代码: #include<iostream> #include& ...
- AspectJ的基本使用
参考: https://my.oschina.net/itblog/blog/208067
- androidd 程序默认安装位置和数据存储位置(公用和私用)
默认安装位置: android App 安装到外置SD卡中,缓解手机内置内存的压力: <manifest xmlns:android="http://schemas.android.c ...
- Android M新特性之APP Link
The Android M Developer Preview introduces support for App Links, which improves upon existing link ...
- OC之0801
1,字符串 字符串的创建:两种常用初始化方式 NSString *str=[[NSString alloc]initWithFormat:@"i am a boy"]; NSStr ...
- Ubuntu中查看32还是64
安装ubuntu在pc上,不推荐在32位pc安装64位操作系统,64位pc安装32位操作系统 方法/步骤 1 按ctrl+shift+t 快捷键,打开终端,输入sudo uname --m ,按下 ...
- ios之AFN上传下载详细步骤(2)
五.AFN .GET\POST > GET请求 // 1.获得请求管理者 AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperation ...
- in_array 查询数组中是否存在某个值
(PHP 4, PHP 5) in_array — 检查数组中是否存在某个值 说明 bool in_array ( mixed $needle , array $haystack [, bool $s ...
- linux下实时监测tomcat关闭并启动
linux下tomcat总是会无故出现自动关闭的情况,在暂时无法解决该问题时,就需要一个东西能实时监测tomcat是否还正常的运行,若发现已关闭时,执行启动命令. 我们可以添加一个shell脚本来实现 ...
- GO RPC
HTTP RPC SERVER CODE package main import ( "errors" "fmt" "net/http" & ...