《Cracking the Coding Interview》——第2章:链表——题目3
2014-03-18 02:25
题目:给定一个单链表中间的节点,删掉那个节点。
解法:把后面节点的数据域拷到当前节点来,然后删除后面那个节点。当前节点不是尾巴,所以后面不为空。
代码:
// 2.2 Remove a node from middle of a linked list
#include <cstdio>
using namespace std; struct ListNode {
int val;
struct ListNode *next;
ListNode(int x): val(x), next(nullptr) {};
}; class Solution {
public:
void deleteThatNode(ListNode *ptr) {
if (ptr != nullptr && ptr->next != nullptr) {
struct ListNode *tmp = ptr->next;
ptr->val = tmp->val;
ptr->next = tmp->next;
delete tmp;
}
}
}; int main()
{
int i;
int n, k;
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 a node from middle of the list
scanf("%d", &k);
k = k < ? : k;
k = k > n ? n : k;
ptr = head;
for (i = ; i < k; ++i) {
ptr = ptr->next;
}
sol.deleteThatNode(ptr); // 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章:链表——题目3的更多相关文章
- 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 ...
随机推荐
- Canvas 中drawImage 绘制不出图片
在使用Canvas的drawImage绘制图片时,却发现绘制不出图片,原因是图片是异步加载,图片加载完再绘制. //html <img src="1.png" /> & ...
- org.apache.xmlbeans.XmlException: error: does not close tag
使用myeclipse的jax自动生成webservice , 或者serviceImpl通过@webservice来实现webservice时, 使用soap UI (我测试使用的版本 5.2.1) ...
- 使用ecilpse(Java)调用Matlab代码
1 安装java环境: http://www.oracle.com/technetwork/java/javase/downloads/index.html 下载JDK最新版本并安装,CloudSim ...
- 种类并查集,Poj(1703)
题目链接:http://poj.org/problem?id=1703 第一次做种类并查集,有的地方还不是很清楚,想了一上午,有点明白了,这里记录一下. 这里我参考的红黑联盟的题解. 关键:种类并查集 ...
- eclips新建Maven Web项目
一.创建项目 1.Eclipse中用Maven创建项目 上图中Next 2.继续Next 3.选maven-archetype-webapp后,next 4.填写相应的信息,Packaged是默认创建 ...
- 20145238-荆玉茗 《Java程序设计》第一次实验报告
实验一 Java开发环境的熟悉(Linux + Eclipse) 实验内容 1.使用JDK编译.运行简单的Java程序: 2.使用Eclipse 编辑.编译.运行.调试Java程序. 实验要求 1.没 ...
- openstack cinder api对应的命令行接口
a) Create Volume cinder create 1 --display-name admin-volume1 cinder create --display-na ...
- scrapy Pipeline 练习
class WeatherPipeline(object): def process_item(self, item, spider): print(item) return item #插入到red ...
- git提交远程报错[rejected] master -> master (fetch first)
1 先下载git for windows 然后一直点下一步安装. 2 安装之后,选择git gui.生成ssh 链接 git 公钥. 用命令也可以: $ ssh-keygen -t rsa -C &q ...
- Ubuntu 16.04安装docker(2018年最新)
参考https://blog.csdn.net/bingzhongdehuoyan/article/details/79411479 http://www.cnblogs.com/lighten/p/ ...