《Cracking the Coding Interview》——第2章:链表——题目1
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的更多相关文章
- Cracking the Coding Interview:: 寻找有环链表的环路起始节点
给定一个有环链表,实现一个算法返回环路的开头节点. 这个问题是由经典面试题-检测链表是否存在环路演变而来.这个问题也是编程之美的判断两个链表是否相交的扩展问题. 首先回顾一下编程之美的问题. 由于如果 ...
- Cracking The Coding Interview 2.0 单链表
#include <iostream> #include <string> using namespace std; class linklist { private: cla ...
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- 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 ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- 《Cracking the Coding Interview》——第2章:链表——题目7
2014-03-18 02:57 题目:检查链表是否是回文的,即是否中心对称. 解法:我的做法是将链表从中间对半拆成两条,然后把后半条反转,再与前半条对比.对比完了再将后半条反转了拼回去.这样不涉及额 ...
- 《Cracking the Coding Interview》——第2章:链表——题目6
2014-03-18 02:41 题目:给定一个带有环的单链表,找出环的入口节点. 解法1:用hash来检测重复节点肯定是容易想而且效率也高的好办法. 代码: // 2.6 You have a ci ...
随机推荐
- 笨办法学Python(十三)
习题 13: 参数.解包.变量 在这节练习中,我们将讲到另外一种将变量传递给脚本的方法(所谓脚本,就是你写的 .py 程序).你已经知道,如果要运行 ex13.py,只要在命令行运行 python e ...
- MySQL数据库实验五:数据更新
实验五 数据更新 一.实验目的 掌握数据更新操作的用法. 二.实验环境 三.实验示例 1.往基本表SC中插入元组. ① INSERT INTO S(S#,SNAME,AGE,SEX) VA ...
- ABAP Netweaver和Hybris里获得内存使用统计数据
ABAP Netweaver 事物码ST06 Hybris 每隔5秒钟,Hybris Administration console会发起一个到Java后台的AJAX查询请求: 这个5秒的时间间隔定义在 ...
- POJ-2139 Six Degrees of Cowvin Bacon---Floyd
题目链接: https://vjudge.net/problem/POJ-2139 题目大意: 给定一些牛的关系,他们之间的距离为1. 然后求当前这只牛到每只牛的最短路的和,除以 n - 1只牛的最大 ...
- 45. 腾讯面试题: 使用hashmap 插入数据,怎么样依照插入数据的顺序输出数据
题目:使用hashmap 插入数据,怎么样依照插入数据的顺序输出数据 分析: 使用hashmap插入数据,数据的顺序会改变.能够写个小程序试试. 那怎么样依照插入的顺序输出呢? 方法一: 这是我第一时 ...
- 优先队列(priority_queue)的cmp,POJ(2051)
sort()函数的cmp为函数,priority_queue的cmp为类,具体写法是: struct Node { int i,j; } node[]; struct cmp { bool opera ...
- node.js 练习3 调用函数
(1)创建n3-1.js,并输入代码 (2)创建User.js ,并输入代码 (3)运行cmd (4)在浏览器上查看 (5) 再次查看cmd
- idea中将项目转换成Maven项目
第一步:项目右键->Add Framework... 选择maven ok 这样就成功转换成了一个maven项目
- 神经网络系列学习笔记(四)——神经网络之RNN学习笔记
不同于传统的FNNs(Feed-forward Neural Networks,前向反馈神经网络),RNNs引入了定向循环,能够处理那些输入之间前后关联的问题. RNNs的目的是用来处理序列数据. 具 ...
- is和==,encode和decode
0.编码解码 >encode和decode a = "你好" s = a.encode("GBK") print(s) # b'\xc4\xe3\xba\ ...