2014-03-18 02:16

题目:给定一个未排序的单链表,去除其中的重复元素。

解法1:不花额外空间,使用O(n^2)的比较方法来找出重复元素。

代码:

 // 2.1 Remove duplicates from a linked list
// inefficient without hashing space
#include <cstdio>
#include <unordered_set>
using namespace std; struct ListNode {
int val;
struct ListNode *next;
ListNode(int x): val(x), next(nullptr) {};
}; class Solution {
public:
void removeDuplicates(ListNode *head) {
if (head == nullptr) {
return;
} struct ListNode *ptr, *del, *tmp; ptr = head;
while (ptr != nullptr && ptr->next != nullptr) {
del = ptr;
while (del->next != nullptr) {
if (ptr->val == del->next->val) {
tmp = del->next;
del->next = tmp->next;
delete tmp;
} else {
del = del->next;
}
}
ptr = ptr->next;
}
}
}; int main()
{
int i;
int n;
int val;
struct ListNode *head, *ptr;
Solution sol; while (scanf("%d", &n) == && n > ) {
// create a linked list
ptr = head = nullptr;
for (i = ; i < n; ++i) {
scanf("%d", &val);
if (head == nullptr) {
head = ptr = new ListNode(val);
} else {
ptr->next = new ListNode(val);
ptr = ptr->next;
}
} // remove duplicates from the list
sol.removeDuplicates(head); // print the list
printf("%d", head->val);
ptr = head->next;
while (ptr != nullptr) {
printf("->%d", ptr->val);
ptr = ptr->next;
}
printf("\n"); // delete the list
while (head != nullptr) {
ptr = head->next;
delete head;
head = ptr;
}
} return ;
}

解法2:使用额外空间的话,可以用unordered_set作为hash工具,进行重复元素的查找,效率更高。

代码:

 // 2.1 Remove duplicates from a linked list
// efficient with hashing space
#include <cstdio>
#include <unordered_set>
using namespace std; struct ListNode {
int val;
struct ListNode *next;
ListNode(int x): val(x), next(nullptr) {};
}; class Solution {
public:
void removeDuplicates(ListNode *head) {
if (head == nullptr) {
return;
} unordered_set<int> us;
struct ListNode *ptr, *del; us.insert(head->val);
ptr = head;
while (ptr->next != nullptr) {
if (us.find(ptr->next->val) != us.end()) {
// duplicate value
del = ptr->next;
ptr->next = del->next;
delete del;
} else {
ptr = ptr->next;
us.insert(ptr->val);
}
} us.clear();
}
}; int main()
{
int i;
int n;
int val;
struct ListNode *head, *ptr;
Solution sol; while (scanf("%d", &n) == && n > ) {
// create a linked list
ptr = head = nullptr;
for (i = ; i < n; ++i) {
scanf("%d", &val);
if (head == nullptr) {
head = ptr = new ListNode(val);
} else {
ptr->next = new ListNode(val);
ptr = ptr->next;
}
} // remove duplicates from the list
sol.removeDuplicates(head); // print the list
printf("%d", head->val);
ptr = head->next;
while (ptr != nullptr) {
printf("->%d", ptr->val);
ptr = ptr->next;
}
printf("\n"); // delete the list
while (head != nullptr) {
ptr = head->next;
delete head;
head = ptr;
}
} return ;
}

《Cracking the Coding Interview》——第2章:链表——题目1的更多相关文章

  1. Cracking the Coding Interview:: 寻找有环链表的环路起始节点

    给定一个有环链表,实现一个算法返回环路的开头节点. 这个问题是由经典面试题-检测链表是否存在环路演变而来.这个问题也是编程之美的判断两个链表是否相交的扩展问题. 首先回顾一下编程之美的问题. 由于如果 ...

  2. Cracking The Coding Interview 2.0 单链表

    #include <iostream> #include <string> using namespace std; class linklist { private: cla ...

  3. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  4. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  5. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  6. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  7. 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 ...

  8. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  9. 《Cracking the Coding Interview》——第2章:链表——题目7

    2014-03-18 02:57 题目:检查链表是否是回文的,即是否中心对称. 解法:我的做法是将链表从中间对半拆成两条,然后把后半条反转,再与前半条对比.对比完了再将后半条反转了拼回去.这样不涉及额 ...

  10. 《Cracking the Coding Interview》——第2章:链表——题目6

    2014-03-18 02:41 题目:给定一个带有环的单链表,找出环的入口节点. 解法1:用hash来检测重复节点肯定是容易想而且效率也高的好办法. 代码: // 2.6 You have a ci ...

随机推荐

  1. graphql 项目搭建(二)

    一.Express基本框架 1.新建一个文件夹gql-server vscode 打开项目,在终端下输入yarn init -y 生成package.json 包(如果没安装yarn ,npm也一样, ...

  2. c++ priority_queue

    1.默认为大顶堆 #include <iostream> #include <queue> using namespace std; int main() { priority ...

  3. QT学习之QScript

    QT中有解析Json的一个类叫QScript.貌似还有一个QJson,但听说解析的方便性不如QScript,具体没有深入探究,这里仅简单记录一下QScript的使用. 首先,主要使用到的类有QScri ...

  4. 贪心,POJ(2709)

    题目链接:http://poj.org/problem?id=2709 解题报告: #include <stdio.h> #include <algorithm> #inclu ...

  5. A. Kyoya and Colored Balls_排列组合,组合数

    Codeforces Round #309 (Div. 1) A. Kyoya and Colored Balls time limit per test 2 seconds memory limit ...

  6. nodejs 的一些PHP函数库

    http://locutus.io/php/ nodejs 的一些PHP函数库 PHP extensions in JavaScript array array_change_key_case arr ...

  7. 注解@Component,@Controller,@Service,@Repository简单了解

    Spring 自 2.0 版本开始,陆续引入了一些注解用于简化 Spring 的开发.@Repository注解便属于最先引入的一批,它用于将数据访问层 (DAO 层 ) 的类标识为 Spring B ...

  8. java导入、导出

    一.介绍 当前B/S模式已成为应用开发的主流,而在企业办公系统中,常常有客户这样子要求:你要把我们的报表直接用Excel打开(电信系统.银行系统).或者是:我们已经习惯用Excel打印.这样在我们实际 ...

  9. 自己写js库,怎么支持AMD

    最近我打算把之前做项目写的一些工具集成到一个js库中,但是库既要在普通环境正常运行,又要在AMD环境下不暴露全局变量.一时间挺头疼的.随即我参考了一些现在流行的库的源码.学着写了一下,感觉还不错. 既 ...

  10. ipython notebook的使用

    刚开始使用python,用的是ipython notebook,感觉很好用. 写的程序主要是处理文件的,读写txt文件,生成xml文件,其中参考http://www.cnblogs.com/wangs ...