原文:

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?

译文:

删除链表中的重复元素,

另外,如果不使用临时存储空间怎么做?

解答:

如果空间没有限制,那么用一个map来标记链表中出现过的元素,然后从头到尾扫一遍链表,把出现过的删除就行了。时间复杂度O(n)。

如果限制空间,那么可以设置两个指针n、m,当n指向某个元素时,m把该元素后面与它相同的元素删除, 时间复杂度O(n2)。

  1. import java.util.HashMap;
  2.  
  3. public class List {
  4. int data;
  5. List next;
  6.  
  7. public List(int d) {
  8. this.data = d;
  9. this.next = null;
  10. }
  11.  
  12. void appendToTail(int d) {
  13. List end = new List(d);
  14. List n = this;
  15. while (n.next != null) {
  16. n = n.next;
  17. }
  18. n.next = end;
  19. }
  20.  
  21. void print() {
  22. List n = this;
  23. System.out.print("{");
  24. while (n != null) {
  25. if (n.next != null)
  26. System.out.print(n.data + ", ");
  27. else
  28. System.out.println(n.data + "}");
  29. n = n.next;
  30. }
  31. }
  32.  
  33. void removeDuplicate() {
  34. HashMap<Integer, Boolean> map = new HashMap<Integer, Boolean>();
  35. List n = this;
  36. map.put(Integer.valueOf(n.data), true);
  37. while (n.next != null) {
  38. if (map.containsKey(Integer.valueOf(n.next.data))) {
  39. n.next = n.next.next;
  40. } else {
  41. map.put(Integer.valueOf(n.next.data), true);
  42. n = n.next;
  43. }
  44. }
  45. }
  46.  
  47. void removeDuplicate1() {
  48. List n = this;
  49. List m;
  50. while (n != null) {
  51. m = n;
  52. while (m.next != null) {
  53. if(m.next.data == n.data)
  54. m.next = m.next.next;
  55. else
  56. m = m.next;
  57. }
  58. n = n.next;
  59. }
  60. }
  61.  
  62. public static void main(String args[]) {
  63. List list = new List(0);
  64. list.appendToTail(1);
  65. list.appendToTail(2);
  66. list.appendToTail(2);
  67. list.appendToTail(3);
  68. list.appendToTail(3);
  69. list.appendToTail(4);
  70. list.appendToTail(1);
  71. list.appendToTail(2);
  72. list.appendToTail(0);
  73. list.print();
  74. //list.removeDuplicate();
  75. list.removeDuplicate1();
  76. list.print();
  77. }
  78. }

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

  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. python_Opencv_绘图

    opencv中也可以用一些函数来绘图 直接上源码,例子: # -*- coding: utf-8 -*- import numpy as np import cv2 # 黑色的图片 img=np.ze ...

  2. [CSS3] CSS Media Queries

    Using CSS media queries allows you to design responsive layout in your web apps and website. We will ...

  3. UITableViewStyleGrouped顶部留白问题

    我这样创建, ? 1 2 self.tableView = [[UITableView alloc] initWithFrame:                       CGRectMake(0 ...

  4. JQuery window、document、 body (转)

    转自:http://www.cnblogs.com/luhe/archive/2012/11/08/2760619.html 我电脑屏幕分辨率:1440 * 900   最大化浏览器,刷新浏览器 al ...

  5. [转] gdb中忽略信号处理

    信号(Signals) 信号是一种软中断,是一种处理异步事件的方法.一般来说,操作系统都支持许多信号.尤其是UNIX,比较重要应用程序一般都会处理信号.UNIX定义了许 多信号,比如SIGINT表示中 ...

  6. [转] 用source命令执行脚本和用sh执行脚本之间的区别

    from: http://blog.csdn.net/david_xtd/article/details/8012627 问题: 有很多方式可以执行脚本, 1).source test.bsh 2). ...

  7. Html5选择图片并及时预览图片

    以往想要实现图片预览基本都是先传至服务器后等返回链接地址才能进行预览,使用Html5选择图片并及时预览图片的代码如下,使用起来更爽了. <!DOCTYPE html> <html l ...

  8. 关于禁止在 .NET Framework 中执行用户代码。启用 "clr enabled" 配置选项

    这个问题是我新装好sql2008r2以后,我把服务器上的数据库还原到本地,取代码里跟踪测试的时候,出现的这个问题. 然后我在网上找了之后在sql里直接新建查询执行如下语句: exec sp_confi ...

  9. nyoj 44

    //nyoj 44 //和上面一题一样,求子串和,但是代码非常简洁..... 时间复杂度为n #include <iostream> using namespace std; int ma ...

  10. hdu 2203

    题意: 子串问题 水题,只要把母串*2,然后比较...... 感觉我好懒....没有自己写函数...... 反正我不是勤快的人......... AC代码: #include <iostream ...