Write code to partition a linked list around a value x, such that all nodes less than x
come before alt nodes greater than or equal to.

分割一个链表,将值小于x的节点全部放在其他节点的前面。

解答:

我们可以创建两个不同的链表,其中一个用来连接比x小的节点,另外一个则连接大于等于x的节点,然后合并两个链表即可。

public class Main {
public static void appendToTail(List head, int d) {
List end = new List(d);
List n = head;
while (n.next != null) {
n = n.next;
}
n.next = end;
} public static void print(List head) {
List n = head;
System.out.print("{");
while (n != null) {
if (n.next != null)
System.out.print(n.data + ", ");
else
System.out.println(n.data + "}");
n = n.next;
}
} public static List partition(List head, int x) {
List small = null;
List large = null;
List largeHead = null;
List smallHead = null;
while (head != null) {
List next = head.next;
head.next = null;
if (head.data < x) {
if (small == null) {
small = head;
smallHead = small;
} else {
small.next = head;
small = small.next;
}
} else {
if (large == null) {
large = head;
largeHead = large;
} else {
large.next = head;
large = large.next;
}
}
head = next;
}
if(small == null)
{
return largeHead;
}
small.next = largeHead;
return smallHead;
} public static void main(String args[]) {
List list = new List(0);
appendToTail(list, 9);
appendToTail(list, 8);
appendToTail(list, 7);
appendToTail(list, 6);
appendToTail(list, 5);
appendToTail(list, 4);
appendToTail(list, 3);
appendToTail(list, 2);
appendToTail(list, 1);
print(list);
print(partition(list, 5));
}
} class List {
int data;
List next; public List(int d) {
this.data = d;
this.next = null;
} public List() {
}
}

Cracking the coding interview--Q2.4的更多相关文章

  1. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  2. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  3. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  4. Cracking the Coding Interview(Stacks and Queues)

    Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...

  5. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  6. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  7. 《Cracking the Coding Interview》——第13章:C和C++——题目6

    2014-04-25 20:07 题目:为什么基类的析构函数必须声明为虚函数? 解法:不是必须,而是应该,这是种规范.对于基类中执行的一些动态资源分配,如果基类的析构函数不是虚函数,那么 派生类的析构 ...

  8. 《Cracking the Coding Interview》——第5章:位操作——题目7

    2014-03-19 06:27 题目:有一个数组里包含了0~n中除了某个整数m之外的所有整数,你要设法找出这个m.限制条件为每次你只能用O(1)的时间访问第i个元素的第j位二进制位. 解法:0~n的 ...

  9. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

  10. 《Cracking the Coding Interview 》之 二叉树的创建 与 遍历(非递归+递归version)

    #include <iostream> #include <cstdio> #include <vector> #include <stack> #de ...

随机推荐

  1. springMVC整合jedis+redis

    http://www.cnblogs.com/zhengbn/p/4140549.html 前两天写过 springMVC+memcached 的整合,我从这个基础上改造一下,把redis和sprin ...

  2. lesson10:hashmap变慢原因分析

    下面的英文描述了String.hashCode()方法,在特定情况下,返回值为0的问题: Java offers the HashMap and Hashtable classes, which us ...

  3. Ubuntu 添加桌面快捷方式

    使用Linux Desktop Entry创建 ~$ cd ~/Desktop/ ~$ vim AndroidStudio.desktop 插入以下代码 [Desktop Entry] Encodin ...

  4. 阿里云服务器[教程3]一键安装php+mysql+ftp+nginx环境

    直接看地址 http://help.aliyun.com/manual?spm=0.0.0.0.F5PPZs&helpId=129

  5. 第四节:教你如何快速让浏览器兼容ES6特性

    写在正文前,本来这一节的内容应该放在第二节更合适,因为当时就有同学问ES6的兼容性如何,如何在浏览器兼容ES6的特性,这节前端君会介绍一个抱砖引玉的操作案例. 为什么ES6会有兼容性问题? 由于广大用 ...

  6. vim 学习相关记录

    VIM 相关内容****************** vim 的三个模式: 编辑模式 --> 输入模式 --> 末行模式 编辑模式: 通常键入键盘值被理解成一个操作; 如: dd(删除行) ...

  7. Android Studio学习随笔-移动动画的实现

    在上一篇博客我已经讲述了三种事件的实现方法,而现在我用复用方法来实现控件的自动移动,当然要实现控件的移动,先得在activity_main.xml文件中放置一个控件,此处我放置的是一个button控件 ...

  8. 安装Stomp扩展时错误提示error: 'zend_class_entry' has no member named 'default_properties'

    在安装stomp扩展时, 有这样的提示 error: 'zend_class_entry' has no member named 'default_properties' 交待下安装上下文, sto ...

  9. TCO 2015 Round 1B DIV1 500 概率题

    [题意]现在有一些线索,每个线索被发现的概率p[i],如果线索i被知道,那么其他线索也可能会被知道,用vector<string> c给出,c[i][j]='Y'表示知道i这个线索,j这个 ...

  10. new Date()在IE,谷歌,火狐上的一些注意项

    1.new Date()在IE浏览器上IE9以上的可以直接使用new Date("yyyy-MM-dd"),但是在IE8上的时候就要使用new Date("yyyy/MM ...