Implement an algorithm to delete a node in the middle of a singly 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

删除链表中的一个节点,但并没有给出链表的头结点,而是只能访问要删除的这个节点。

解答:

将待删除的下一个节点copy到当前节点,然后删除下一个节点。要单独考虑待删除的是链表尾或者链表头的情况。链表头没有特别影响,而如果是链表尾的话,一开始想的是直接将该节点赋值为null,但是测试时发现没有效果。 看书中说如果是链表尾是不能删除的,要向面试官指出这个问题,或者将该节点的数据标记成一个特殊的值表示这个节点是个虚假不用的节点。

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 boolean revomeNode(List node) {
if (node == null || node.next == null) {
return false;
} else {
node.data = node.next.data;
node.next = node.next.next;
return true;
}
} public static void main(String args[]) {
List list = new List(0);
appendToTail(list, 1);
appendToTail(list, 2);
appendToTail(list, 3);
appendToTail(list, 4);
appendToTail(list, 5);
appendToTail(list, 6);
appendToTail(list, 7);
appendToTail(list, 8);
appendToTail(list, 9);
print(list);
List node = list;
for(int i = 0; i < 9; i++)
node = node.next;
System.out.println(revomeNode(node));
print(list);
}
} class List {
int data;
List next; public List(int d) {
this.data = d;
this.next = null;
}
}

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

  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. 如何理解oracle 11g scan ip

    如何理解oracle 11g scan ip   在11.2之前,client链接数据库的时候要用vip,假如你的cluster有4个节点,那么客户端的tnsnames.ora中就对应有四个主机vip ...

  2. Android代码混淆

    混淆器(ProGuard) --- 混淆器通过删除从未用过的代码和使用晦涩名字重命名类.字段和方法,对代码进行压缩,优化和混淆.结果是一个比較小的.apk文件,该文件比較难进行逆向project.因此 ...

  3. [转] linux新的API signalfd、timerfd、eventfd使用说明

    http://blog.csdn.net/gdutliuyun827/article/details/8460417 三种新的fd加入linux内核的的版本: signalfd:2.6.22 time ...

  4. Python爬虫:获取糗事百科笑话

    为了收集笑话也是挺拼的,我就不相信你所有的都看过了.还有,请问哪位仁兄能指点之下怎么把网上抓取到的图片写到word里面,捉摸了好久都没弄出来.   糗百不需要登录,html直接解析,只要在reques ...

  5. EasyUI-datagrid获取编辑行的数据

    可以在页面对datagrid的数据直接进行修改,然后提交到数据库,但是要求在提交前获取datagrid的所有行的数据.API提供了getData方法 最后这样写才搞定 var    arr=$(‘#d ...

  6. Gradle命令详解与导入第三方包

    Android Studio + Gradle的组合用起来非常方便,很多第三方开源项目也早都迁移到了Studio,为此今天就来介绍下查看.编译并导入第三方开源项目的方法. Sublime + Term ...

  7. VB 活动添加item元素

    'ListView1.Columns.Clear() 'ListView1.Columns.Add("", 0, HorizontalAlignment.Center) 'List ...

  8. 二维码生成Demo

    在C#中直接引用ThoughtWorks.QRCode.dll 类, 下载 dll 类 http://file.111cn.net/download/2013/06/29/20120516165420 ...

  9. ViewPager循环广告位的实现

    1.如何实现循环播放 2.如何实现自动循环 如何实现循环播放 现在网上实现循环播放都是在adapter的getCount()方法返回一个较大的值并且instantiateItem(ViewGroup ...

  10. Xcode快捷键 (本人总结常用的)

    以下分类显示最常用的快捷键: 1. 文件 CMD + N: 新文件 CMD + SHIFT + N:     新项目 CMD + O: 打开 CMD + S: 保存 CMD + SHIFT + S: ...