《Cracking the Coding Interview》——第2章:链表——题目2
2014-03-18 02:24
题目:给定一个单链表,找出倒数第K个节点。
解法:让一个指针先走K步,然后俩指针一起走到尽头。当然也可以先走到尽头数出链表的长度,然后第二次少走K步。其实耗费的工夫是一样的,但貌似总有人觉得第一种方法很巧妙很优美。
代码:
// 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:
ListNode* findKthNode(ListNode *head, int k) {
if (head == nullptr || k < ) {
return head;
}
struct ListNode *p1, *p2; p1 = p2 = head;
int i;
for (i = ; i < k; ++i) {
p2 = p2->next;
if (p2 == nullptr) {
return head;
}
}
while (p2 != nullptr) {
p1 = p1->next;
p2 = p2->next;
} return p1;
}
}; 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;
}
} // find the kth node from the end of the list
scanf("%d", &k);
ptr = sol.findKthNode(head, k);
printf("%d\n", ptr->val); /*
// 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章:链表——题目2的更多相关文章
- 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 ...
随机推荐
- MySQL入门很简单: 12 MYSQL 用户管理
1. 权限表 安装MySQL会自动安装一个名为mysql的数据库,存储权限表: user表, db表,host表,table_priv表,columns_priv表,proc_priv表等. 1)us ...
- java集合框架——List
一.List接口概述 List有个很大的特点就是可以操作角标. 下面开始介绍List接口中相对于Collection接口比较特别的方法.在Collection接口中已经介绍的方法此处就不再赘述. 1. ...
- Uva 12169 不爽的裁判 模运算
题目链接:https://vjudge.net/contest/156903#problem/B 题意: 有一个递推公式 : a,b都不是已知的,给出了 x1,x3,x5.... 求x2,x4,x6. ...
- 引用类型(三):Function类型
一. Function类型函数实际上是对象.每个函数都是Function类型都实例,而且都与其他引用类型一样具有属性和方法.由于函数是对象,因此函数名实际上也是一个指向函数对象都指针.1.函数通常是使 ...
- python 面向对象(三)--继承和多态
在OOP程序设计中,当我们定义一个class的时候,可以从某个现有的class继承,新的class称为子类(Subclass),而被继承的class称为基类.父类或超类(Base class.Supe ...
- 关于IDataReader.GetSchemaTable的一些事情
http://stackoverflow.com/questions/1574492/how-does-getschematable-work The implementation of IDataR ...
- fast rcnn的实例
http://www.cnblogs.com/louyihang-loves-baiyan/p/4906690.html https://saicoco.github.io/object-detect ...
- Linux 启动、停止、重启tomcat工具(Shell脚本)
1. 启动 #!/bin/bash pids=`ps -ef | grep java | grep -w tomcat | awk '{print $2}'` #pids=`ps -ef | gr ...
- 【转】Druid连接池一个设置引发的血案
https://my.oschina.net/haogrgr/blog/224010 今天在一台配置很低的机器上运行批量更新的程序~~~ 大概跑了三十分钟~~~这配置~~~这程序~~~ 然后华丽丽的报 ...
- python-类对象以列表切片模式操作
#类对象以列表切片模式操作 class Person: def __init__(self): self.cache=[] def __setitem__(self, key, value): #修改 ...