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 ...
随机推荐
- 数组去重Array
var aee3=[31,42,13,19,5,11,8,13,40,39,1,8,44,15,3]; Array.prototype.unqu2=function(){ this.sort(); v ...
- 【Qt for Android】OpenGL ES 绘制彩色立方体
Qt 内置对OpenGL ES的支持.选用Qt进行OpenGL ES的开发是很方便的,很多辅助类都已经具备.从Qt 5.0開始添加了一个QWindow类,该类既能够使用OpenGL绘制3D图形,也能够 ...
- 《数字图像处理原理与实践(MATLAB版)》一书之代码Part2
本文系<数字图像处理原理与实践(MATLAB版)>一书之代码系列的Part2(P43~80),代码运行结果请參见原书配图,建议下载代码前阅读下文: 关于<数字图像处理原理与实践(MA ...
- Ubuntu12.04下使用virtualbox4.3.12 amd64安装XP系统教程
首先第一步打开已安装好的Virtualbox4.3.12,效果图例如以下: 第二步:点击新建进入新建虚拟电脑界面,填写名称,选择类型和版本号(我这里使用的三XP 64bit): 第三步:选择内存大小, ...
- JQuery开发工具和插件
最近的研究jquery.为大家介绍几款开发工具.能够帮助你提高开发的效率. 1.Dreamweaver Dreamweaver是建立在WEB站点和应用程序的专业工具. 将可视化工具.应用程序开发功能和 ...
- qt安装遇到的错误
/usr/bin/ld: cannot find -lXrender collect2: ld returned 1 exit status make[1]: *** [../../../../lib ...
- Eclipse上运行Python,使用PyDev
转自:http://www.ibm.com/developerworks/cn/opensource/os-cn-ecl-pydev/index.html 级别: 初级 郑 伟芳 (zhengwf@c ...
- Cocos2d-x发展---更改父的颜色、透明度的子节点上
标题手段:当我们改变父节点或透明时的颜色.默认是不会影响孩子的节点. 作为交换组看到朋友说可以通过设置相关的参数变化的子节点来实现属性的效果,看了看源代码,记录下来: 引擎版本号为:2. ...
- ZTESoft 持续集成 编年史 之 持续集成探索---平台选择
2012 年 7.8 月份,我们逐渐了解了持续集成的概念,同时我们家庭作坊的dailybuild方式不断爆出各种问题,并且已经无法满足日益增长的各种需求. 我们开始探索持续集成的不同实现方式,首先我们 ...
- 通过Java字节码发现有趣的内幕之String篇(上)(转)
原文出处: jaffa 很多时候我们在编写Java代码时,判断和猜测代码问题时主要是通过运行结果来得到答案,本博文主要是想通过Java字节码的方式来进一步求证我们已知的东西.这里没有对Java字节码知 ...