careercup-链表 2.2
2.2 实现一个算法,找到单链表中倒数第k个节点。
这道题的考点在于我们怎么在一个单链表中找到倒数第n个元素? 由于是单链表,所以我们没办法从最后一个元素数起,然后数n个得到答案。 但这种最直观的思路显然是没错的,那我们有没有办法通过别的方式,从最后的元素数起数 n个来得到我们想要的答案呢。
这个次序颠倒的思路可以让我们联想到一种数据结构:栈。
我们如果遍历一遍单链表,将其中的元素压栈,然后再将元素一一出栈。那么, 第n个出栈的元素就是我们想要的。
那我们是不是需要显式地用栈这么一个结构来做这个问题呢?答案是否。看到栈,我们应当 要想到递归,它是一种天然使用栈的方式。所以,第一种解决方案用递归,让栈自己帮我 们从链表的最后一个元素数起。
思路很简单,如果指向链表的指针还未空,就不断递归。当指针为空时开始退递归,这个过 程n不断减1,直接等于1,即可把栈中当前的元素取出。代码如下:
node *pp;
int nn;
void findNthToLast1(node *head){
if(head==NULL) return;
findNthToLast1(head->next);
if(nn==) pp = head;
--nn;
}
虽然我们没办法从单链表的最后一个元素往前数,但如果我们维护两个指针, 它们之间的距离为k。然后,我将这两个指针同步地在这个单链表上移动,保持它们的距离 为k不变。那么,当第二个指针指到空时,第一个指针即为所求。很tricky的方法, 将这个问题很漂亮地解决了。
C++实现代码:
#include<iostream>
#include<new>
using namespace std; struct ListNode
{
int val;
ListNode *next;
ListNode(int x):val(x),next(NULL) {}
}; void createList(ListNode *&L)
{
int arr[]= {,,,,,,,,,};
int i;
ListNode *p=NULL;
for(i=; i<; i++)
{
ListNode *tmp=new ListNode(arr[i]);
if(L==NULL)
{
L=tmp;
p=tmp;
}
else
{
p->next=tmp;
p=tmp;
}
}
} ListNode *findKNode(ListNode *L,int k)
{
if(L==NULL)
return NULL;
ListNode *p=L;
ListNode *q=L;
int count=;
while(q)
{
if(count==k)
{
p=p->next;
q=q->next;
}
else
{
count++;
q=q->next;
}
}
if(count==k)
return p;
else
return NULL;
} int main()
{
ListNode *head=NULL;
createList(head);
ListNode *p=head;
while(p)
{
cout<<p->val<<" ";
p=p->next;
}
cout<<endl;
p=findKNode(head,);
if(p)
cout<<p->val<<endl;
}
careercup-链表 2.2的更多相关文章
- [CareerCup] 2.1 Remove Duplicates from Unsorted List 移除无序链表中的重复项
2.1 Write code to remove duplicates from an unsorted linked list.FOLLOW UPHow would you solve this p ...
- [CareerCup] 2.2 Kth to Last Element of Linked List 链表的倒数第k个元素
2.2 Implement an algorithm to find the kth to last element of a singly linked list. 这道题让我们求链表中倒数第k个元 ...
- [CareerCup] 2.3 Delete Node in a Linked List 删除链表的节点
2.3 Implement an algorithm to delete a node in the middle of a singly linked list, given only access ...
- [CareerCup] 2.4 Partition List 划分链表
2.4 Write code to partition a linked list around a value x, such that all nodes less than x come bef ...
- [CareerCup] 2.6 Linked List Cycle 单链表中的环
2.6 Given a circular linked list, implement an algorithm which returns the node at the beginning of ...
- [CareerCup] 2.7 Palindrome Linked List 回文链表
2.7 Implement a function to check if a linked list is a palindrome. LeetCode上的原题,参见我之前的博客Palindrome ...
- [CareerCup] 4.4 Create List at Each Depth of Binary Tree 二叉树的各层创建链表
4.4 Given a binary tree, design an algorithm which creates a linked list of all the nodes at each de ...
- [CareerCup] 17.13 BiNode 双向节点
17.13 Consider a simple node-like data structure called BiNode, which has pointers to two other node ...
- 二叉树系列 - 二叉搜索树 - 线性时间内把有序链表转化为BST
引言 本文来自于Google的一道题目: how to merge two binary search tree into balanced binary search tree. how to me ...
- Careercup - Google面试题 - 5735304249999360
2014-05-03 23:18 题目链接 原题: Insert a element in a sorted circular linked list 题目:题意简单明了,向一个有序的循环单向链表中插 ...
随机推荐
- leetcode 第一题 Two Num java
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- BZOJ 1725: [Usaco2006 Nov]Corn Fields牧场的安排
Description Farmer John新买了一块长方形的牧场,这块牧场被划分成M列N行(1<=M<=12; 1<=N<=12),每一格都是一块正方形的土地.FJ打算在牧 ...
- 【网络流24题】 No.22~24
接下来几题就写写题解吧.不是很想打了. 22. 输入文件示例input.txt4 21 2 7 36 5 8 37 8 10 59 6 13 9 输出文件示例output.txt17 最长不相交路径. ...
- 【网络流24题】No.18 分配问题 (二分图最佳匹配 费用流|KM)
[题意] 有 n 件工作要分配给 n 个人做.第 i 个人做第 j 件工作产生的效益为 cij . 试设计一个将n 件工作分配给 n 个人做的分配方案, 使产生的总效益最大. 输入文件示例input. ...
- ANDROID_MARS学习笔记_S03_008_GOOGLEMAP2
一.简介 二.代码1.xml(1)main.xml <?xml version="1.0" encoding="utf-8"?> <Linea ...
- cocos2d-x拖动滑块控件CCControlSlider
#include "GameMusicSetting.h" bool GameMusicSetting::init() { if(!CCLayer::init()) { retur ...
- windows 挂载windows 共享盘为本地磁盘
我们在设置数据库自动备份时,为了数据的安全往往需要直接将数据备份到远程服务器上.在Linux可以通过NFS挂载来实现,在Windows平台可以直接通过net use+subst来实现将远程服务器的目录 ...
- 通过 Azure 媒体服务进行高速编码
Milan Gada Azure 媒体服务首席项目经理 关于 Azure 媒体服务,客户问的最多的一个问题是能否进行高速编码.这个问题现在已经有了肯定的答案,目前有三种不同类型的 Encodin ...
- xcode duplicate symbol _GAD_MD5 解决方法
添加了mobi的广告平台后,在Device状态打包时,出现此错误. duplicate symbol _GAD_MD5 in: 解决方法: Targets ->Build Setting 中设 ...
- 畅通工程续 HDOJ--1874
畅通工程续 Time Limit : 3000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submiss ...