如何准备

Linked list questions are extremely common These can range from simple (delete a node ina linked list) to much more challenging Either way, we advise you to be extremely comfortable with the easiest questions Being able to easily manipulate a linked
list in the simplestways will make the tougher linked list questions much less tricky With that said, we present some “must know” code about linked list manipulation You should be able to easily writethis code yourself prior to your interview

链表是面试中非常常见的问题。问题难度有难有易。无论题目如何,我们都建议你先练熟简单题目。掌握了简单链表题,面对那些难题的时就会轻松一点。所以下面列出了一些链表题中必备的代码。

Creating a Linked List:

创建一个链表:

NOTE: When you’re discussing a linked list in an interview, make sure to understand whether it is a single linked list or a doubly linked list.

1 class Node {2 Node next = null;3 int data;4 public Node(int d) { data = d; }5 void appendToTail(int d) {6 Node end = new Node(d);7 Node n = this;8 while (n.next != null) { n = n.next; }9 n.next = end;10 }11 }

注意:在面试的时候写链表,一定要先问清楚是写单链表还是双链表!

Deleting a Node from a Singly Linked List

在单链表中删除一个节点

1 Node deleteNode(Node head, int d) {2 Node n = head;3 if (n.data == d) {4 return head.next; /* moved head */5 }6 while (n.next != null) {7 if (n.next.data == d) {8 n.next = n.next.next;9 return head; /* head didn’t change */10 }11 n = n.next;12
}13 }

2 1 Write code to remove duplicates from an unsorted linked list;

FOLLOW UP

How would you solve this problem if a temporary buffer is not allowed

2.1从一个未排序的链表中删除重复元素

进阶:如果不能使用额外的空间怎么办?
2.1解答:

如果可以使用哈希表的话,那么检查每一个元素是否重复,然后移除。


如果不能额外使用空间的话,我们就需要两个指针来遍历数组:current用来正常的遍历;runner则用来检查元素是否重复。runner只为一个元素检查一次是否重复,因为每一次runner会删除和元素的所有重复元素。

2.2 Implement an algorithm to find the nth to last element of a singly linked list

2.2 实现算法查找单链表中的倒数第n个元素。

注意:这个问题强烈的暗示递归算法,但是这里才采用一种更巧妙的方法。像这样类似的问题就,一定要多思考看看。能不能用非递归的方法代替递归的方法。
2.2解答:

这里我们假设链表的长度至少为n。

算法描述:

(1) 创建两个指针p1和p2,均指向链表头。

(2) 让p2增加n-1次,使得p2指向从链表头第n个元素(使得p1和p2之间距离为n)

(3) 若p2->next为空,则返回p1;不为空则同时增加p1,p2。(如果p2->next为空,则表示p1所指的元素为倒数第n个,因为p1,p2的距离为n。)

(4) 重复(3)

2.3 Implement an algorithm to delete a node in the middle of a single linked list, given only access to that node

EXAMPLE Input: the node ‘c’ from the linked list a->b->c->d->e  Result: nothing is returned, but the new linked list looks like a->b->d->e

2.3 实现一个算法,只给你链表中间的一个元素(没有链表头),将其从链表中删除。

例如:

输入:节点 c (原链表为 a->b->c->d->e)

输出:没有任何返回值。但链表变成 a->b->d->e
2.3解答:

本题的解答只是把输入的下一个元素拷贝到输入的这个元素中以完成删除输入的元素。(译者注:原文的代码没有释放下一元素的空间。)
注意:这样的方法并不能删除链表的最后一个元素。这一点需要和你的面试官说清楚。算法有缺陷没有关系,大胆的告诉你的面试官,他们喜欢看到你提出这些。至于怎么解决可以和你的面试官讨论。

2.4 You have two numbers represented by a linked list, where each node contains a single digit The digits are stored in reverse order, such that the 1’s digit is at the head of the list Write a function that adds the two numbers and returns the
sum as a linked list  EXAMPLE Input: (3 -> 1 -> 5) + (5 -> 9 -> 2)  Output: 8 -> 0 -> 8

2.4 两个用链表表示的数字,最低位在表头。写一个函数对两个这样的链表求和,采用相同的形式返回结果。

例如: (3 -> 1 -> 5) + (5 -> 9 -> 2)  返回  8 -> 0 -> 8
2.4解答:

本题可以采用递归的方式逐位的做加法。算法流程:

2

2 5 Given a circular linked list, implement an algorithm which returns node at the begin- ning of the loop  DEFINITION Circular linked list: A (corrupt) linked list in which a node’s next pointer points to an earlier node, so as to make a loop in
the linked list  EXAMPLE  input: A -> B -> C -> D -> E -> C [the same C as earlier]  output: C

solution:

If we move two pointers, one with speed 1 and another with speed 2, they will end up meeting if the linked list has a loop   Why?  Think about two cars driving on a track—the faster car will always pass the slower one! The tricky part here is finding the start
of the loop   Imagine, as an analogy, two people racing around a track, one running twice as fast as the other   If they start off at the same place, when will they next meet?  They will next meet at the start of the next lap

Now, let’s suppose Fast Runner had a head start of k meters on an n step lap When will

they next meet?  They will meet k meters before the start of the next lap   (Why? Fast Runner would have made k + 2(n - k) steps, including its head start, and Slow Runner would have made n - k steps   Both will be k steps before the start of the loop )

Now, going back to the problem, when Fast Runner (n2) and Slow Runner (n1) are moving around our circular linked list, n2 will have a head start on the loop when n1 enters   Specifically, it will have a head start of k, where k is the number of nodes before
the loop   Since n2 has a head start of k nodes, n1 and n2 will meet k nodes before the start of the loop 

So, we now know the following:

1     Head is k nodes from LoopStart (by definition) 

2     MeetingPoint for n1 and n2 is k nodes from LoopStart (as shown above) 

Thus, if we move n1 back to Head and keep n2 at MeetingPoint, and move them both at the same pace, they will meet at LoopStart

2.5 给一个带有环的链表,设计算法查找这个环的起点。

环的定义:链表中的某一元素的下一个元素为之前的一个元素。

例如:输入 A -> B -> C -> D -> E -> C 输出 C
2.5 解答:

如果在链表中有两个指针,从表头开始p1以每次一个节点的速度前进,p2每次2个。那么这两个指针一定在链表的环中相遇。想想两辆车以不同的速度在一个环形跑道上运动,那么他们肯定会在跑道上再次相遇。

这个问题最难的部分就在于如何超出这个环开始的节点。假想下如果是在一个圆形的跑道上p2以两倍的速度于p1同时从起点出发,他们将会哪里相遇。还是在起点!

现在我们再假设如果p2超前起跑线k米起跑,他们第一次在哪里相遇呢?在起跑线后面k米处。(为什么?假设p2过了起跑线之后跑了x和p1相遇,那么p2跑了l-k+x,其中l为跑道的长度,至相遇时p1也跑了x。根据相遇时消耗的时间相等得方程 (l-k-x)/2=x/1,解得 x = l-k,也就是在起跑线后k处相遇。)


那重新回到问题本身,当p1指针刚进入环的时候,p2在环中恰好领先p1指正k个节点,k为链表头节点到环开始处的距离。根据之前的结论,当p1、p2相遇的时候,他们都相遇环开始节点k个距离。

那么我们得到如下结论:

(1)表头距离环开始处k节点

(2)p1,p2距离环开始处也是k个节点。

如果当p1、p2相遇之后,将p1至于表头然后两个指针都采用1的速度移动的话。那么相遇处即为环开始处。

【IT笔试面试题整理】链表的更多相关文章

  1. Java笔试面试题整理第六波(修正版)

    转载至:http://blog.csdn.net/shakespeare001/article/details/51330745 作者:山代王(开心阳) 本系列整理Java相关的笔试面试知识点,其他几 ...

  2. Java笔试面试题整理第四波

    转载至:http://blog.csdn.net/shakespeare001/article/details/51274685 作者:山代王(开心阳) 本系列整理Java相关的笔试面试知识点,其他几 ...

  3. Java笔试面试题整理第八波

    转载至:http://blog.csdn.net/shakespeare001/article/details/51388516 作者:山代王(开心阳) 本系列整理Java相关的笔试面试知识点,其他几 ...

  4. Java笔试面试题整理第五波

    转载至:http://blog.csdn.net/shakespeare001/article/details/51321498 作者:山代王(开心阳) 本系列整理Java相关的笔试面试知识点,其他几 ...

  5. Java笔试面试题整理第三波

    转载至:http://blog.csdn.net/shakespeare001/article/details/51247785 作者:山代王(开心阳) 本系列整理Java相关的笔试面试知识点,其他几 ...

  6. Java笔试面试题整理第二波

    转载至:http://blog.csdn.net/shakespeare001/article/details/51200163 作者:山代王(开心阳) 本系列整理Java相关的笔试面试知识点,其他几 ...

  7. Java笔试面试题整理第一波

    转载至:http://blog.csdn.net/shakespeare001/article/details/51151650 作者:山代王(开心阳) 本系列整理Java相关的笔试面试知识点,其他几 ...

  8. 【IT笔试面试题整理】给定二叉树,给每层生成一个链表

    [试题描述]定义一个函数,给定二叉树,给每层生成一个链表 We can do a simple level by level traversal of the tree, with a slight ...

  9. 【IT笔试面试题整理】删除无序链表中重复的节点

    [试题描述]定义一个函数,输入一个链表,删除无序链表中重复的节点 [参考代码] 方法一: Without a buffer, we can iterate with two pointers: &qu ...

  10. 【IT笔试面试题整理】判断链表是否存在环路,并找出回路起点

    [试题描述]定义一个函数,输入一个链表,判断链表是否存在环路,并找出回路起点 Circular linked list: A (corrupt) linked list in which a node ...

随机推荐

  1. java基础知识-原码,反码,补码

    1.正数:原码,反码,补码:都一样. 2.负数:和正数的储存方式不同,负数都是以补码形式存储的. <1>负数的补码 把负数的原码除了符号位取反后再+1. <2>负数的原码 把对 ...

  2. CentOS ntp同步

    新装的CentOS系统服务器可能设置了错误的,需要调整时区并调整时间. 如下是CentOS系统使用NTP来从一个时间服务器同步 把当前时区调整为上海就是+8区,想改其他时区也可以去看看/usr/sha ...

  3. 在Delphi中处理word文档与数据库的互联 1

    在Delphi中处理word文档与数据库的互联 ---- 目前,Delphi被越来越多的人选中作为MIS系统开发中的前台工具.在以Delphi为前台,一些大型数据库为后台的MIS系统中,图形的处理不可 ...

  4. AlexNet详解3

    Reference. Krizhevsky A, Sutskever I, Hinton G E. ImageNet Classification with Deep Convolutional Ne ...

  5. asp.net 子应用程序/虚拟目录 session共享

    最近遇到了一个问题,我做的asp.net mvc应用程序要作为一个子应用程序部署到几个站点中,需要在本应用程序中获取站点的session值. 已经使用了session state server,并设置 ...

  6. [NewCode 4] 替换空格

    题目描述 请实现一个函数,将一个字符串中的空格替换成"%20".例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 最直接的方式, ...

  7. .NET Core下开源任务调度框架Hangfire的Api任务拓展(支持秒级任务)

    HangFire的拓展和使用 看了很多博客,小白第一次写博客. 最近由于之前的任务调度框架总出现问题,因此想寻找一个替代品,之前使用的是Quartz.Net,这个框架方便之处就是支持cron表达式适合 ...

  8. AngularJs创建自定义Service

    AngularJs可以创建自定义的service.下面的自定义service实现一个double倍数的服务: 参考下面语法: app.service('double', function () { t ...

  9. 【转】ABP使用Mysql数据库

    原文地址:https://www.cnblogs.com/LonelyCode/p/6477065.html 1.先安装Mysql的包,EntityFramework和Web项目都需要安装 2.修改W ...

  10. 【译】AI 让科技公司变得更强大吗

    机器学习可能是当今技术中最重要的基本趋势.由于机器学习的基础是数据 - 大量的数据 - 很常见的是,人们越来越担心已经拥有大量数据的公司会变得更强大.这有一定的道理,但是以相当狭窄的方式,同时ML也看 ...