1 题目

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.

接口

ListNode partition(ListNode head, int x)

2 思路

给定一个x的值,小于x都放在大于等于x的前面,并且不改变链表之间node原始的相对位置。example中 4->3->5都是大于等3的数,这保持了他们原来的相对位置 。
思路1:使用链表最常用的双指针,一个指向当前小于x的最后一个元素,一个进行往前扫描。如果元素大于x,那么继续前进,否则,要把元素移到前面,并更新第一个指针。
思路2:
  1. new两个新链表,一个用来创建所有大于等于x的链表,一个用来创建所有小于x的链表。
  2. 遍历整个链表时,当当前node的val小于x时,接在小链表上,反之,接在大链表上。这样就保证了相对顺序没有改变,而仅仅对链表做了与x的比较判断。
  3. 最后,把小链表接在大链表。

复杂度

2种方法的时间和空间复杂度一样。
Time:O(n)
Space: O(1)

3 代码

     // 思路2:dummy1 小于x的linkedlist; dummy2 大于等于x的linkedlist.
public ListNode partition(ListNode head, int x) {
ListNode dummy1 = new ListNode(-1);
ListNode dummy2 = new ListNode(-2);
ListNode t1 = dummy1, t2 = dummy2, pCur = head;
while (pCur != null) {
ListNode pTmp = pCur.next;
if (pCur.val < x) {
t1.next = pCur;
t1 = t1.next;
} else {
t2.next = pCur;
t2 = t2.next;
t2.next = null;
}
pCur = pTmp;
}
t1.next = dummy2.next;
return dummy1.next;
}

4 总结

  • Two Pointer的思想
  • 这是普通的链表操作,应该熟练掌握。
  • 思路1实现细节要多些,用一个头结点、2个指针可以完成。
  • 思路2实现简单明了。

5 参考

lc面试准备:Partition List的更多相关文章

  1. lc面试准备:Remove Duplicates from Sorted List

    1 题目 Given a sorted linked list, delete all duplicates such that each element appear only once. For ...

  2. lc面试准备:Implement Stack using Queues

    1 题目 Implement the following operations of a stack using queues. push(x) -- Push element x onto stac ...

  3. lc面试准备:Implement Queue using Stacks

    1 题目 Implement the following operations of a queue using stacks. push(x) -- Push element x to the ba ...

  4. lc面试准备:Invert Binary Tree

    1 题目 Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 接口: public TreeNod ...

  5. lc面试准备:Power of Two

    1 题目 Given an integer, write a function to determine if it is a power of two. 接口 boolean isPowerOfTw ...

  6. lc面试准备:Repeated DNA Sequences

    1 题目 All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: &quo ...

  7. lc面试准备:Number of 1 Bits

    1 题目 Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also ...

  8. lc面试准备:Reverse Bits

    1 题目 Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represente ...

  9. lc面试准备:Regular Expression Matching

    1 题目 Implement regular expression matching with support for '.' and '*'. '.' Matches any single char ...

随机推荐

  1. Java基础知识强化之IO流笔记26:FileInputStream / FileOutputStream 复制mp4视频的案例

    1.  需求:把D:\\English.mp4 复制到当前项目目录下copy.mp4 代码示例: package com.himi.filecopy; import java.io.FileInput ...

  2. 【bzoj1212】 [HNOI2004]L语言

    题目描述 标点符号的出现晚于文字的出现,所以以前的语言都是没有标点的.现在你要处理的就是一段没有标点的文章. 一段文章T是由若干小写字母构成.一个单词W也是由若干小写字母构成.一个字典D是若干个单词的 ...

  3. asp gridview批量删除和全选

    本人新手刚学asp.net   全选和删除也是引用了他人的代码试了一试可以实现,感觉很好,就发了上来. 前台代码 <asp:GridView ID="GridView1" r ...

  4. php跨服务器信息获取之cURL

    原文地址:php跨服务器信息获取之cURL作者:陌上花开 其实有几种方式 $content = file_get_contents("http://www.nettuts.com" ...

  5. XmlHttpRequest 使用

    1. IE7以后对xmlHttpRequest 对象的创建在不同浏览器上是兼容的.下面的方法是考虑兼容性的,实际项目中一般使用Jquery的ajax请求,可以不考虑兼容性问题. function ge ...

  6. A题笔记(6)

    No. 3040 代码量好少,主要考到数学知识 唯一需要注意的是变量的类型 int -2147483648 ~ +2147483647 (4 Bytes) long 在32位机器中 int 类型 和 ...

  7. Graphics类绘制图形

    1. 画直线 void drawLine(int startX,int startY,int endX,int endY); 四个参数分别为:起始点的x坐标和y坐标以及终点的x坐标和y坐标,该方法用于 ...

  8. 召回率与准确率[ZZ]

    最近一直在做相关推荐方面的研究与应用工作,召回率与准确率这两个概念偶尔会遇到,知道意思,但是有时候要很清晰地向同学介绍则有点转不过弯来. 召回率和准确率是数据挖掘中预测.互联网中的搜索引擎等经常涉及的 ...

  9. php 解决大流量网站访问量问题

    当一个网站发展为知名网站的时候(如新浪,腾讯,网易,雅虎),网站的访问量通常都会非常大,如果使用虚拟主机的话,网站就会因为访问量过大而引起 服务器性能问题,这是很多人的烦恼,有人使用取消RSS等错误的 ...

  10. ecshop 函数列表大全

    lib_time.phpgmtime() P: 获得当前格林威治时间的时间戳 /$0server_timezone() P: 获得服务器的时区 /$0local_mktime($hour = NULL ...