LeetCode86 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.(Medium)
For example,
Given 1->4->3->2->5->2 and x = 3,
return 1->2->2->4->3->5.
分析:
把链表归并,其思路就是先开两个链表,把小于x的值接在链表left后面,大于x的值接在链表right后面;
然后把链表left的尾部与链表right的头部接在一起。
注意:链表right的尾部next赋值为nullptr。
代码:
/**
* 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 dummy1();
ListNode dummy2();
ListNode* left = &dummy1;
ListNode* right = &dummy2;
while (head != nullptr) {
if (head -> val < x) {
left -> next = head;
left = left -> next;
}
else {
right -> next = head;
right = right -> next;
}
head = head -> next;
}
left -> next = dummy2.next;
right -> next = nullptr;
return dummy1.next;
}
};
LeetCode86 Partition List的更多相关文章
- Leetcode86. Partition List分隔链表(双指针)
给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前. 你应当保留两个分区中每个节点的初始相对位置. 示例: 输入: head = 1->4-&g ...
- LeetCode 86. 分隔链表(Partition List)
86. 分隔链表 86. Partition List 题目描述 给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前. 你应当保留两个分区中每个节点的 ...
- [Swift]LeetCode86. 分隔链表 | Partition List
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- Partition:增加分区
在关系型 DB中,分区表经常使用DateKey(int 数据类型)作为Partition Column,每个月的数据填充到同一个Partition中,由于在Fore-End呈现的报表大多数是基于Mon ...
- Partition:Partiton Scheme是否指定Next Used?
在SQL Server中,为Partition Scheme多次指定Next Used,不会出错,最后一次指定的FileGroup是Partition Scheme的Next Used,建议,在执行P ...
- Partition:分区切换(Switch)
在SQL Server中,对超级大表做数据归档,使用select和delete命令是十分耗费CPU时间和Disk空间的,SQL Server必须记录相应数量的事务日志,而使用switch操作归档分区表 ...
- sql 分组取最新的数据sqlserver巧用row_number和partition by分组取top数据
SQL Server 2005后之后,引入了row_number()函数,row_number()函数的分组排序功能使这种操作变得非常简单 分组取TOP数据是T-SQL中的常用查询, 如学生信息管理系 ...
- Oracle Partition Outer Join 稠化报表
partition outer join实现将稀疏数据转为稠密数据,举例: with t as (select deptno, job, sum(sal) sum_sal from emp group ...
- SQLServer中Partition By 函数的使用
今天群里看到一个问题,在这里概述下:查询出不同分类下的最新记录.一看这不是很简单的么,要分类那就用Group By;要最新记录就用Order By呗.然后在自己的表中试着做出来: 首先呢我把表中的数据 ...
随机推荐
- 如何处理HTML5新标签的浏览器兼容问题?
方法一 : 1.使用静态资源的html5shiv包 <!--[if lt IE9]> <script src="http://cdn.static.runoob.com/l ...
- 非常好理解的KNN算法示例
参考链接:https://www.joinquant.com/post/2227?f=study&m=math:一只兔子帮你理解KNN https://www.joinquant.com/po ...
- ES6学习笔记之字符串的扩展
字符串的for of ES6 为字符串添加了遍历器接口,使得字符串可以被for...of循环遍历. const str='abcd'; for(let s of str){ console.log(s ...
- C++/CLI 创建WinForm程序
本文演示下用CLR创建一个简单的winform程序,IDE:VS2015 可以参考另一篇文章:http://blog.csdn.net/wcc27857285/article/details/7813 ...
- homebrew长时间停在Updating Homebrew 这个步骤
在国内的网络环境下使用 Homebrew 安装软件的过程中可能会长时间卡在 Updating Homebrew 这个步骤. 例:执行 brew install composer 命令 ➜ ~ brew ...
- UE4物理模块(三)---碰撞查询(上)
在前一文中介绍了如何在UE4中创建简单碰撞或者直接使用其mesh表示的复杂碰撞: Jerry:UE4物理模块(二)---建立物体碰撞zhuanlan.zhihu.com 那么在拿到碰撞之后,就可以进 ...
- php pdo操作数据库的方法
PDO 安装 你可以通过 PHP 的 phpinfo() 函数来查看是否安装了PDO扩展. 1.在 Unix /linux系统上安装 PDO 在Unix上或Linux上你需要添加以下扩展: exten ...
- Javascript-正则表达式常用字符集及方法
正则表达式修饰符(修饰符 可以在全局搜索中不区分大小写) i(ignoreCase)执行对大小写不敏感的匹配 g (global) 执行全局匹配(查找所有匹配而非在找到第一个匹配后停止) m( ...
- 说说a标签的onclick和href
在平时我们一般会在列表中的最后一列给加上操作功能,一般的操作功能是修改和删除,这个操作我们可以通过a标签来实现其功能. <a class="pn-opt" href=&quo ...
- Wndows下Apache+php+Mysql环境的搭建及其涉及的知识
一.安装Apache 1. 在网上搜索以下3个文件,以及找一个地方新建一个文件夹 好吧,这里有下载链接:http://pan.baidu.com/s/1hr9IdSS 文件夹内有:apache,mys ...