Partition List -- LeetCode
原题链接: http://oj.leetcode.com/problems/partition-list/
这是一道链表操作的题目,要求把小于x的元素按顺序放到链表前面。我们仍然是使用链表最经常使用的双指针大法,一个指向当前小于x的最后一个元素,一个进行往前扫描。假设元素大于x,那么继续前进,否则,要把元素移到前面,并更新第一个指针。这里有一个小细节,就是假设不须要移动(也就是已经是接在小于x的最后元素的后面了),那么仅仅须要继续前进就可以。算法时间复杂度是O(n),空间仅仅须要几个辅助变量,是O(1)。代码例如以下:
public ListNode partition(ListNode head, int x) {
if(head == null)
return null;
ListNode helper = new ListNode(0);
helper.next = head;
ListNode walker = helper;
ListNode runner = helper;
while(runner.next!=null)
{
if(runner.next.val<x)
{
if(walker!=runner)
{
ListNode next = runner.next.next;
runner.next.next = walker.next;
walker.next = runner.next;
runner.next = next;
}
else
runner = runner.next;
walker = walker.next;
}
else
{
runner = runner.next;
}
}
return helper.next;
}
这道题思路比較清晰,只是还是有点细节的,第一次写可能不easy全然写对,能够练习练习。
Partition List -- LeetCode的更多相关文章
- Partition List ——LeetCode
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- Partition List leetcode java
题目: Given a linked list and a value x, partition it such that all nodes less than x come before node ...
- 561. Array Partition I - LeetCode
Question 561. Array Partition I Solution 题目大意是,给的数组大小是2n,把数组分成n组,每组2个元素,每个组取最小值,这样就能得到n个值,怎样分组才能使这n个 ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- Solution to LeetCode Problem Set
Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...
- [LeetCode] Partition Equal Subset Sum 相同子集和分割
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- [LeetCode] Partition List 划分链表
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- [LeetCode]题解(python):086 - Partition List
题目来源 https://leetcode.com/problems/partition-list/ Given a linked list and a value x, partition it s ...
- LeetCode: Palindrome Partition
LeetCode: Palindrome Partition Given a string s, partition s such that every substring of the partit ...
随机推荐
- webservice之cxf样例
整理參考于网上资源: http://wenku.baidu.com/link?url=MbPPOKCficQCAwSZduszpMFSD3f8xCKeNz6YUtwFS1TkHharz1aPPfkXD ...
- ThinkPHP的连贯操作方法中field方法
ThinkPHP的连贯操作方法中field方法有很多的使用技巧,field方法主要目的是标识要返回或者操作的字段,下面详细道来. 1.用于查询 在查询操作中field方法是使用最频繁的. $Model ...
- Andriod中绘(画)图----Canvas的使用具体解释
转载请注明出处:http://blog.csdn.net/qinjuning 因为在网络上找到关于Canvas的使用都比較抽象,或许是我的逻辑思维不太好吧,总是感觉理解起来比較困难, 尤其是对 ...
- HADOOP2.6
LINUX下HADOOP2.6.0集群环境的搭建 本文旨在提供最基本的,可以用于在生产环境进行Hadoop.HDFS分布式环境的搭建,对自己是个总结和整理,也能方便新人学习使用. 基础环境 JDK的安 ...
- set echo on/off,set term on/off,set feedback off,set heading off命令(转)
1.term命令: 当和SPOOL命令联合使用时,可以取消SQLPLUS输出,查询结果仅仅存在于假脱机文件中 set term on:查询结果既显示于假脱机文件中,又在SQLPLUS中显示: s ...
- 【剑指offer】复制的复杂链条
转载请注明出处:http://blog.csdn.net/ns_code/article/details/26154691 题目描写叙述: 输入一个复杂链表(每一个节点中有节点值,以及两个指针,一个指 ...
- Knockout应用开发指南 第五章:创建自定义绑定
原文:Knockout应用开发指南 第五章:创建自定义绑定 创建自定义绑定 你可以创建自己的自定义绑定 – 没有必要非要使用内嵌的绑定(像click,value等).你可以你封装复杂的逻辑或行为,自定 ...
- 《循序渐进Oracle》部分笔记
1.不要用户名/密码 直接/as sysdba 是操作系统认证方式,改变安全方式 sqlnet.ora 里SQLNET.AUTHENTICATION_SERVICES=(NTS)表示启动操作系统认证; ...
- java 通过sftp服务器上传下载删除文件
最近做了一个sftp服务器文件下载的功能,mark一下: 首先是一个SftpClientUtil 类,封装了对sftp服务器文件上传.下载.删除的方法 import java.io.File; imp ...
- ecshop中getAll ,getOne ,getRow的区别
ecshop的数据库抽象层其实就是在模仿adodb $GLOBALS['db']->getAll($sql);//以二维关联数组返回所有数据 $GLOBALS['db']->getOne( ...