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的更多相关文章

  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. P2082 区间覆盖(加强版)

    题目 #include<iostream> #include<algorithm> #include<cstring> using namespace std; s ...

  2. rabbitmq安装使用

    使用 http://www.open-open.com/lib/view/open1325131828249.html ubuntu:apt-get install erlang-noxsudo ap ...

  3. 第45章 DCMI—OV2640摄像头—零死角玩转STM32-F429系列

    第45章     DCMI—OV2640摄像头 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com ...

  4. 在Linux上部署Kettle环境

    首先我们有一个正常安装的,桌面版的Linux. Kettle的应用程序是Linux版本与Windows版本在同一个文件夹下共存的,所以可以直接把本机上的Kettle解压,通过FTP工具上传到Linux ...

  5. Oracle SCN与时间的相互转换

    1.SCN转换成时间 select scn_to_timestamp(current_scn) from v$database; 2.时间转换成SCN select timestamp_to_scn( ...

  6. sql的使用

    1.自动获取最新订单号 select concat('XJDD',DATE_FORMAT(now(),'%Y%m%d'), LPAD(( FOR )) , max(SUBSTRING(inquiryn ...

  7. shell脚本结构化语句

    本文中记录一下shell中的两种循环语句:for和while for循环 for循环是linux shell中最常用的结构,for循环有三种结构:1.列表for循环.2.不带列表for循环.3.C风格 ...

  8. 8-2 开发接口 (入参是json格式)

    1.开发入参事json格式的接口 import json import tools import flask from .check_session import check_session serv ...

  9. System.gc()日志分析

    打开日志:运行配置---XX:+PrintGCDetails 示例程序: package com.test; public class Test { private Object instance = ...

  10. node服务端渲染(完整demo)

    简介 nodejs搭建多页面服务端渲染 技术点 koa 搭建服务 koa-router 创建页面路由 nunjucks 模板引擎组合html webpack打包多页面 node端异步请求 服务端日志打 ...