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?

这道题让我们移除无序链表中的重复项,在LeetCode中有两道类似的题是Remove Duplicates from Sorted List 移除有序链表中的重复项 和 Remove Duplicates from Sorted List II 移除有序链表中的重复项之二。这两道都是针对有序链表的,而这道题是针对无序链表的,其实难度也不是很大。很多对于链表的处理的题都需要在头结点前建立一个dummy node,目的是为了防止头结点被移除,没法返回新的头结点位置。而这道题不用,因为此题让我们删除重复的节点,不是全删掉,而是会保留一个,那么不管头结点有没有重复项,都会保留下来。这题我们可以用哈希表来解,思路是对于每一个节点,如果在哈希表中存在,则删掉,若不存在,则加入哈希表,参见代码如下:

class Solution {
public:
ListNode *deleteDuplicates(ListNode *head) {
ListNode *pre = NULL, *cur = head;
int m[] = {};
while (cur) {
if (m[cur->val] > ) {
pre->next = cur->next;
} else {
++m[cur->val];
pre = cur;
}
cur = cur->next;
}
return head;
}
};

这道题的Follow Up让我们不要用额外空间,即空间复杂度应为O(1),那么我们需要两个while循环来解,同时需要两个指针,第一个指针指向一个节点,第二个指针从下一个为位置开始遍历到链表末尾,遇到相同的就删掉。以此类推直到第一个指针完成链表的遍历即可删掉所有的重复项,整体的思路和冒泡排序有些类似,但是这种方法的时间复杂度为O(n2),是一种以时间来换取空间的方法,参见代码如下:

class Solution {
public:
ListNode *deleteDuplicates(ListNode *head) {
ListNode *pre = head, *cur = head;
while (pre) {
cur = pre->next;
while (cur) {
if (cur->val == pre->val) {
pre->next = cur->next;
}
cur = cur->next;
}
pre = pre->next;
}
return head;
}
};

[CareerCup] 2.1 Remove Duplicates from Unsorted List 移除无序链表中的重复项的更多相关文章

  1. [LeetCode] Remove Duplicates from Sorted List 移除有序链表中的重复项

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  2. [LeetCode] 83. Remove Duplicates from Sorted List 移除有序链表中的重复项

    Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1 ...

  3. LeetCode 83. Remove Duplicates from Sorted List (从有序链表中去除重复项)

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  4. 【LeetCode每天一题】Remove Duplicates from Sorted List(移除有序链表中的重复数字)

    Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1 ...

  5. [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项 II

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  6. [LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)

    https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C% ...

  7. [LeetCode] Remove Duplicates from Sorted List II 移除有序链表中的重复项之二

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  8. [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项之二

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  9. **80. Remove Duplicates from Sorted Array II 删除排序数组中的重复项 II

    1. 原始题目 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件 ...

随机推荐

  1. Java基础--常用正则匹配符号(必背,必须背,死都要背)

    1.字母:匹配单个字母 (1)A:表示匹配字母A: (2)\\:匹配转义字符“\”: (3)\t:匹配转义字符“\t”: (4)\n:匹配转义字符“\n”: 2.一组字符:任意匹配里面的一个单个字符: ...

  2. 开始玩mondrian

    官网:http://community.pentaho.com/projects/mondrian/ 官方编译的包:https://sourceforge.net/projects/mondrian/ ...

  3. python 连接 mysql

    http://blog.csdn.net/yelbosh/article/details/7498641 数据库的连接 模块引入之后我们就需要和数据库进行连接了,实例代码如下: db = MySQLd ...

  4. 《第一行代码——Android》

    <第一行代码——Android> 基本信息 作者: 郭霖 丛书名: 图灵原创 出版社:人民邮电出版社 ISBN:9787115362865 上架时间:2014-7-14 出版日期:2014 ...

  5. nodejs 安装及部署遇到的问题

    Error: ENOENT, stat 'C:\Users\PC_Name\AppData\Roaming\npm PC_Name是机器名 解决方法:在Roaming文件夹下创建一个叫npm的空文件夹 ...

  6. ASP.NET导出bdf文件

    1.导出助手类 using System;using System.IO;using System.Data;using System.Data.OleDb;using System.Web;usin ...

  7. Ajax详解及其案例分析------如何获得Ajax对象,使用Ajax对象发送GET和POST请求,校验用户名,POST和GET请求时的乱码处理,实现级联的下拉列表

    本节主要内容预览: 1 获得Ajax对象 2 使用Ajax对象发送GET请求 3 使用Ajax对象发送POST请求 4 使用Ajax校验用户名 5 POST请求时的乱码处理 6 GET请求时的乱码处理 ...

  8. 【转】LINUX 5 常用ftp telnet配置

    LINUX 5 常用ftp telnet配置 一.解决远程登陆乱码问题 目标:在xwindow和其console中使用中文界面,在纯console中使用英文 在/etc/profile最后加上一行 e ...

  9. [转]jquery-confirm

    本文转自:http://craftpip.github.io/jquery-confirm/ Practical Uses These features can practically be used ...

  10. [转]Ionic – Mobile UI Framework for PhoneGap/Cordova Developers

    本文转自:http://devgirl.org/2014/01/20/ionic-mobile-ui-framework-for-phonegapcordova-developers/ Ionic i ...