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 ...
随机推荐
- UDP vs. TCP
UDP vs. TCP 原文:UDP vs. TCP,作者是Glenn Fiedler,专注于游戏网络编程相关工作多年. 说在最前面的话 翻译这篇文章的初衷:我在工作中根本接触不到网络游戏编程,但是我 ...
- Andorid Clip 实现自定义的进度条效果实例
Android该系统提供了一个水平进度条为我们展现了运行使用进展情况,水平进度条显示用于运行进度Clip Drawable技术 下面我们通过一个具体的例子来说明Clip Drawable使用. 还有我 ...
- Dan计划:重新定义人生的10000个小时 - 阮一峰的网络日志
Dan计划:重新定义人生的10000个小时 - 阮一峰的网络日志 Dan计划:重新定义人生的10000个小时
- SWT中在treeview中显示图片
package com.repositoryclient.treeview; import org.eclipse.jface.resource.ImageDescriptor; import org ...
- python手记(48)
#!/usr/bin/env python # -*- coding: utf-8 -*- #http://blog.csdn.net/myhaspl #code:myhaspl@qq.com imp ...
- prepareCall()运行存储过程
CallableStatement 对象为全部的 DBMS 提供了一种以标准形式调用已储存过程的方法.已储存过程储存在数据库中.对已储存过程的调用是 CallableStatement对象所含的内容. ...
- Java流的理解
最近做了一下Socket编程,其中有socket.getInputStream和socket.getOutputStream的问题. 想传输文件,感觉应该用FileInputStream和FileOu ...
- Error与Exception的区别
错误和异常的区别(Error vs Exception) 错误和异常的区别(Error vs Exception) 今天面试问了这样一个问题,"Error" 和 "Exc ...
- eclipse 安装vrapper vim插件
http://vrapper.sourceforge.net/update-site/stable 如果安装不上,设置下代理./window/pereference/network* - manul
- Android 一些错误
android fragment里面放viewpager 嵌套fragment 报错: 解决:在adapter的构造方法里加上 super(fragment.getChildFragmentManag ...