【链表】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 ...
随机推荐
- Ansible 笔记 (3) - 编写 playbook
playbook 相当于多个命令的编排组合然后一起运行,类似写脚本.在学习 playbook 之前需要了解 yaml 格式. 编写playbook的步骤: 定义主机与用户 编写任务列表 执行 play ...
- 1) Apache Maven 's README.txt
Apache Maven What is it? ----------- Maven is a software project management and comprehension tool. ...
- MapGIS Mobile开发
1. 先将Android开发环境配置好(包括Java + Eclipse + Android SDK) 2. 加载API类库(运行MapGIS 10 AndroidSDK.exe可以加载Mobile框 ...
- (网络流 模板 Edmonds-Karp)Drainage Ditches --POJ --1273
链接: http://poj.org/problem?id=1273 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total ...
- Hdu2181 哈密顿绕行世界问题 2017-01-18 14:46 45人阅读 评论(0) 收藏
哈密顿绕行世界问题 Time Limit : 3000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Sub ...
- android图形系统概述
简介 本文讲解的内容是Android4.1以后的系统机制,将从整体上分析Android图形显示系统的结构,不深入分析每一层内部的代码实现,更多的是使用流程图和结构图来让大家理解Android是如何绘制 ...
- Need You Now --Lady Antebellum
战地女神(Lady Antebellum)由女主唱 Hillary Scott.男主唱 Charles Kelley .吉他/键盘手 Dave Haywood,2006夏天在美国乡村音乐重镇纳什维尔组 ...
- Delphi for iOS开发指南(5):在iOS应用程序中使用Calendar组件来选择日期
http://blog.csdn.net/delphiteacher/article/details/8923519 在FireMonkey iOS应用程序中的Calendar FireMonkey使 ...
- Python学习-35.Python中的List Comprehensions(列表解释|列表生成式)
在某些情况下,我们需要对列表进行某些操作,例如对列表中的每一个元素都乘以2,这样一般来说就是遍历每个元素在乘以2.那么写下来就得两行了.而且这会修改原来的列表,如果要求不能修改原来的列表,又得多一行了 ...
- java 反射应用
场景需求最近的一次解析数据包中,因为协议有改变,本来的定长的包,现在变为不定长的.举个例子,本来协议中规定,一个包中,有8个标签,但是每次上来的,不一定都有8个,没有的话,硬件过来的都是0.同时里面也 ...