【链表】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大或等于的节点连成另一条链,然后把两条链连起来。注意一下边界情况(某条链为空)。
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @param {number} x
* @return {ListNode}
*/
var partition = function(head, x) {
if(head==null||head.next==null){
return head;
}
var lHead=null;
var gHead=null; var p=head,pl=null,pg=null,temp=null; while(p){
if(p.val<x){
if(lHead==null){
lHead=p;
pl=p;
}else{
pl.next=p;
pl=pl.next;
}
}else{
if(gHead==null){
gHead=p;
pg=p;
}else{
pg.next=p;
pg=pg.next;
}
}
temp=p;
p=p.next;
temp.next=null;
} if(pg!=null){
pg.next=null;
}
if(lHead!=null){
pl.next=gHead;
return lHead;
}else{
return gHead;
}
};
【链表】Partition List的更多相关文章
- 链表-Partition List
struct ListNode* partition(struct ListNode* head, int x) { struct ListNode *p1=(struct ListNode*)mal ...
- [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 ...
- leetcode 链表 Partition List
Partition List Total Accepted: 19761 Total Submissions: 73252My Submissions Given a linked list and ...
- LeetCode 86. 分隔链表(Partition List)
86. 分隔链表 86. Partition List 题目描述 给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前. 你应当保留两个分区中每个节点的 ...
- Leetcode解题思想总结篇:双指针
Leetcode解题思想总结篇:双指针 1概念 双指针:快慢指针. 快指针在每一步走的步长要比慢指针一步走的步长要多.快指针通常的步速是慢指针的2倍. 在循环中的指针移动通常为: faster = f ...
- LeetCode_算法及数据结构覆盖统计
[输入]共计151道题的算法&数据结构基础数据 (见附录A) [输出-算法]其中有算法记录的共计 97道 ,统计后 结果如下 top3(递归,动态规划,回溯) 递归 动态规划 回溯 BFS ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- LeetCode总结【转】
转自:http://blog.csdn.net/lanxu_yy/article/details/17848219 版权声明:本文为博主原创文章,未经博主允许不得转载. 最近完成了www.leetco ...
- leetcode解题文件夹
点击打开链接点击打开链接点击打开链接參考文献:http://blog.csdn.net/lanxu_yy/article/details/17848219 只是本文准备用超链接的方式连接到对应解答页面 ...
- [LeetCode] Partition List 划分链表
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
随机推荐
- C语言printf的格式
例1 int a = 12345;printf("%6d",a); // 输出6位不够左边补空格printf("%.6d",a); // 输出6位不够左边补0例 ...
- POJ2653判断线段相交
POJ2653 题目大意:按顺序放木棒,问最后所有的木棒中上面没有木棒的木棒的索引是…… 思路:按理说线段相交的题目做的听多了,这个应该不算新鲜,但是这个题,还是让我学到了认真读题,面对这个题很容易想 ...
- opencpu
前端通过它调用后端的R语言,对R函数进行一个封装. 网址:https://github.com/jeroenooms/opencpu.js 使用的是opencpu-0.5.js,对它进行了修改. 1. ...
- 【WinRT】多语言化应用程序名称
在Windows Phone 8的时候,要对应用程序在列表中显示的名称和锁定到开始屏幕的磁贴的名称进行多语言化是十分困难的,需要使用C++建立一个多语言资源库.但是,在WinRT中,这变得简单多了,无 ...
- 检测Linux服务器端口是否开通
现如今云服务器已经是大势所趋,国内比较著名的云服务器厂商有阿里.腾讯,国外有aws,尽管有的公司目前为止还是使用的物理机,但是无论你是使用的云服务器还是物理机,在运行服务时都必不可少的需要监听到指定的 ...
- Maven发布和管理项目
1 什么是Maven? 如果没有Maven,你可能不得不经历下面的过程: 1 如果使用了spring,去spring的官网下载jar包:如果使用hibernate,去hibernate的官网下载Jar ...
- Excel中使用VBA进行度分秒与十进制度的转换
发现Excel的VBA功能真是批量处理的一把利刃,工作中小试牛刀了一把,将Excel中度分秒形式的坐标批量处理成十进制度形式,处理完后用于GIS展点制图. 原Excel数据如下: VBA代码如下: S ...
- asp.net—自定义轻量级ORM
大型项目中ORM的使用已经是相当的频繁.目前.NET(C#)中比较流行的ORM框架也有很多,比如SqlSugar,Dapper,Entity Framework(EF)等. 相信很多有2年以上工作经验 ...
- 【加密算法】AES
一.简介 AES(Advanced Encryption Standard):高级加密标准,是下一代的加密算法标准,速度快,安全级别高. 用AES加密2000年10月,NIST(美国国家标准和技术协会 ...
- C# 实现图片压缩
代码: private static ImageCodecInfo GetImageCodecInfo(ImageFormat imageFormat) { ImageCodecInfo[] imag ...