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. 第1章 Python基础之字符编码

    阅读目录 一.什么是字符编码 二.字符编码分类 三.字符编码转换关系 3.1 程序运行原理 3.2 终极揭秘 3.3 补充 总结 回到顶部 一.什么是字符编码 计算机要想工作必须通电,也就是说'电'驱 ...

  2. ISAP 简介

    刘汝佳的蓝书上已经给出了大部分,先给上完整代码(以草地排水为例). #include<iostream> #include<algorithm> #include<cst ...

  3. Lucene的中文分词器IKAnalyzer

    分词器对英文的支持是非常好的. 一般分词经过的流程: 1)切分关键词 2)去除停用词 3)把英文单词转为小写 但是老外写的分词器对中文分词一般都是单字分词,分词的效果不好. 国人林良益写的IK Ana ...

  4. SAP-Basic:使用SCC1时提示 TA263“集团被集团副本锁定用于输入”

    错误信息   消息号TA263   解决方法 Step 1.使用SCC4进入"集团管理"界面. Step 2.双击要维护的集团,例如"010",进入详细配置界面 ...

  5. 【足迹C++primer】表达式求值

    表达式求值 /** * 功能:表达式求值(0到9) * 时间:2014年6月15日08:02:31 * 作者:cutter_point */ #include<stdlib.h> #inc ...

  6. [转] C++临时变量的生命周期

    http://www.cnblogs.com/catch/p/3251937.html C++中的临时变量指的是那些由编译器根据需要在栈上产生的,没有名字的变量. 主要的用途主要有两类: 1) 函数的 ...

  7. IDL绘制黑体辐射曲线

    普朗克定律是热红外遥感中常常使用的三大定律之一,描述了黑体辐射能量的情况.绝对黑体的辐射光谱对于研究一切物体的辐射规律具有根本的意义.1900年普朗克引进量子概念,将辐射当做不连续的量子发射,成功地从 ...

  8. yii 载入css or js

    Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl . "/js/TableView.js&q ...

  9. Codeforces 526D - Om Nom and Necklace 【KMP】

    ZeptoLab Code Rush 2015 D. Om Nom and Necklace [题意] 给出一个字符串s,判断其各个前缀是否是 ABABA…ABA的形式(A和B都可以为空,且A有Q+1 ...

  10. 如何学习H264协议

    如何学习h.264协议 首先,我假定你已经具有如下基础: 1 了解基本的视频知识,知道什么是YCbCr/YUV: 2 知道基本的视频压缩原理: 如果这两条还不具备,那么,停一下,补一下课.这方面的相关 ...