Cracking the coding interview--Q2.2
Implement an algorithm to find the kth to last element of a singly linked list.
实现一个算法寻找链表中倒数第K个数..
解答:
关于链表的问题,书中也提到了关键的两个技巧,一个是递归;另一个是 The"Runner"Technique,也就是使用两个指针同时遍历链表的方式。这道题可以分别用这两种方法来解决。
import java.util.HashMap;
public class List {
int data;
List next;
public List(int d) {
this.data = d;
this.next = null;
}
public void appendToTail(int d) {
List end = new List(d);
List n = this;
while (n.next != null) {
n = n.next;
}
n.next = end;
}
public void print() {
List n = this;
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 int nthToLast(int n) {
if (this.next == null) {
if (n == 0)
System.out.println(this.data);
return 0;
}
int i = this.next.nthToLast(n) + 1;
if (i == n)
System.out.println(this.data);
return i;
}
public void nthToLast1(int n) {
List m = this;
List pt = this;
for (int i = 0; i < n; i++){
if(pt.next == null)
return ;
pt = pt.next;
}
while(pt.next != null)
{
m = m.next;
pt = pt.next;
}
System.out.println(m.data);
}
public static void main(String args[]) {
List list = new List(0);
list.appendToTail(1);
list.appendToTail(2);
list.appendToTail(3);
list.appendToTail(4);
list.appendToTail(5);
list.appendToTail(6);
list.appendToTail(7);
list.appendToTail(8);
list.appendToTail(9);
list.print();
list.nthToLast(10);
list.nthToLast1(10);
}
}
Cracking the coding interview--Q2.2的更多相关文章
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- 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 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- 《Cracking the Coding Interview》——第13章:C和C++——题目6
2014-04-25 20:07 题目:为什么基类的析构函数必须声明为虚函数? 解法:不是必须,而是应该,这是种规范.对于基类中执行的一些动态资源分配,如果基类的析构函数不是虚函数,那么 派生类的析构 ...
- 《Cracking the Coding Interview》——第5章:位操作——题目7
2014-03-19 06:27 题目:有一个数组里包含了0~n中除了某个整数m之外的所有整数,你要设法找出这个m.限制条件为每次你只能用O(1)的时间访问第i个元素的第j位二进制位. 解法:0~n的 ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- 《Cracking the Coding Interview 》之 二叉树的创建 与 遍历(非递归+递归version)
#include <iostream> #include <cstdio> #include <vector> #include <stack> #de ...
随机推荐
- Python字符串连接的5种方法
总结了一下Python字符串连接的5种方法: 加号 第一种,有编程经验的人,估计都知道很多语言里面是用加号连接两个字符串,Python里面也是如此直接用 "+" 来连接两个字符串: ...
- 账户管理命令 useradd、groupadd
内容提要: 1. 掌握用户的 增/删/改 命令 2. 掌握组的 增/删/改 命令 组管理 1)groupadd groupadd 用于添加组账号.格式如下: groupadd [-g GID] GRO ...
- Firemonkey的旁门左道[五]
这次讲讲绘制的几种模式吧,不过还是比较浅显,刚接触不久,还实在没这个实力道出个所以来. FMX下,我们可以切换GDI,D2D,GPU这三种模式, 只要通过全局变量就可以轻松搞定. 如何设置 Globa ...
- slidingmenu+fragment实现经常使用的側滑效果(包含Fragment状态的保存)
一.需求 关于fragment的问题,一直想写一篇博客了.应该当初自己也是对这玩意一点都不熟悉到如今也大概知道个日常的使用的地步. 一个側滑的导航栏,内有4个条目.每个选项点击进入相应的界面,每个界面 ...
- cocos2d-x项目过程记录(跨平台iOS和Android)
(原创作品,欢迎转载,注明出处,谢谢:http://www.cnblogs.com/binxindoudou/admin/EditPosts.aspx?postid=3205249) 1.配置环境,重 ...
- Day11 - Mysql and ORM
python 之路,Day11 - python mysql and ORM 本节内容 数据库介绍 mysql 数据库安装使用 mysql管理 mysql 数据类型 常用mysql命令 创建数据库 ...
- python中的generator, iterator, iterabel
先来看看如果遇到一个对象,如何判断其是否是这三种类型: from types import GeneratorType from collectiuons import Iterable, Itera ...
- window.applicationCache事件,介绍
1.关于applicationCache对象 在IE和Google中 为ApplicationCache对象 而在FF中为 OfflineResourceList对象 通过ApplicationCac ...
- MVC使用Exception过滤器自定义处理Action的的异常
1.继承FilterAttribute ,IExceptionFilter自定义处理 /// <summary> /// 登录错误自定义处理 /// </summary> pu ...
- (转)模板引擎类dedetemplate.class.php使用说明
1.概述 织梦的模板标签类似于XML格式,所有的模板都含有定界符,默认情况下是{dede:*}和{/dede:*},“*”代表模板标记名称. 一般情况下{dede:*}和{/dede:*}是成对出现的 ...