原题链接: 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的更多相关文章

  1. Partition List ——LeetCode

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  2. Partition List leetcode java

    题目: Given a linked list and a value x, partition it such that all nodes less than x come before node ...

  3. 561. Array Partition I - LeetCode

    Question 561. Array Partition I Solution 题目大意是,给的数组大小是2n,把数组分成n组,每组2个元素,每个组取最小值,这样就能得到n个值,怎样分组才能使这n个 ...

  4. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  5. Solution to LeetCode Problem Set

    Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...

  6. [LeetCode] Partition Equal Subset Sum 相同子集和分割

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  7. [LeetCode] Partition List 划分链表

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  8. [LeetCode]题解(python):086 - Partition List

    题目来源 https://leetcode.com/problems/partition-list/ Given a linked list and a value x, partition it s ...

  9. LeetCode: Palindrome Partition

    LeetCode: Palindrome Partition Given a string s, partition s such that every substring of the partit ...

随机推荐

  1. hdu 4970 Killing Monsters(数学题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4970 Problem Description Kingdom Rush is a popular TD ...

  2. javascript面向对象程序设计

    在学习js面向对象编程之前,首先须要知道什么是面向对象.面向对象语言都有类的概念,通过它能够创建具有同样属性和方法的对象.但js并没有类的概念,因此js中的对象和其它语言的对象有所不同. js对象能够 ...

  3. httpclient超时总结(转)

    Httpclient超时 背景: 网站这边多次因为httpclient调用超时时间没设置好导致关掉,影响非常不好,而且问题重复出现,查看网络,没有比较明确介绍httpclient所有超时相关的设置(大 ...

  4. http_load安装与测试参数分析 - 追求自由自在的编程 - ITeye技术网站

    http_load安装与测试参数分析 - 追求自由自在的编程 - ITeye技术网站 http_load -p 50 -s 120 urls

  5. php学习之道:php中soap的使用实例以及生成WSDL文件,提供自己主动生成WSDL文件的类库——SoapDiscovery.class.php类

    1. web service普及: Webservice soap wsdl差别之个人见解 Web Service实现业务诉求:  Web Service是真正"办事"的那个,提供 ...

  6. xcode多target

    原文:http://www.codza.com/free-iphone-app-version-from-the-same-xcode-project There are more than 15,0 ...

  7. SE 2014年4月14日

    一. 概述BGP的特点 BGP协议是一种距离矢量协议,基于TCP的179端口,BGP协议不会动态的学习路由,只能将IGP协议学习到的或者静态路由注入到BGP中,成为BGP路由,BGP路由携带有丰富的路 ...

  8. redis作为mysql的缓存服务器(读写分离) (转)

    一.redis简介Redis是一个key-value存储系统.和Memcached类似,为了保证效率,数据都是缓存在内存中.区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录 ...

  9. U8Linux磁盘与文件系统管理

    1.扇区为最小的物理存储单位,每个扇区为512bytes;将扇区组成一个圆,那就是柱面,柱面是分区的最小单位.Linux系统的EX2文件系统:inode的内容用于记录文件的权限与相关属性. 至于blo ...

  10. APUE学习--网络编程(3)

    本篇文章介绍TCP通信. 上文提到传输层的两个协议TCP和UDP,UDP是无连接的已经介绍过,TCP是面向连接的,阐述建立连接和断开连接前先来看下TCP报文头的结构. 报文头在linux的定义在/us ...