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

  1. [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 ...

  2. [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个元 ...

  3. [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 ...

  4. [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 ...

  5. [CareerCup] 2.6 Linked List Cycle 单链表中的环

    2.6 Given a circular linked list, implement an algorithm which returns the node at the beginning of ...

  6. [CareerCup] 2.7 Palindrome Linked List 回文链表

    2.7 Implement a function to check if a linked list is a palindrome. LeetCode上的原题,参见我之前的博客Palindrome ...

  7. [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 ...

  8. [CareerCup] 17.13 BiNode 双向节点

    17.13 Consider a simple node-like data structure called BiNode, which has pointers to two other node ...

  9. 二叉树系列 - 二叉搜索树 - 线性时间内把有序链表转化为BST

    引言 本文来自于Google的一道题目: how to merge two binary search tree into balanced binary search tree. how to me ...

  10. Careercup - Google面试题 - 5735304249999360

    2014-05-03 23:18 题目链接 原题: Insert a element in a sorted circular linked list 题目:题意简单明了,向一个有序的循环单向链表中插 ...

随机推荐

  1. Ubuntu使用wget下载jdk问题

    使用以下命令可下载成功,否则下载下来的可能是一个html文档. wget --no-cookies --no-check-certificate --header "Cookie:gpw_e ...

  2. 思考 Swift 中的 MirrorType 协议

    Swift中的反射非常有限,仅允许以只读方式访问元数据的类型子集.或许 Swift 因有严格的类型检验而不需要反射.编译时已知各种类型,便不再需要进行进一步检查或区分.然后大量的 Cocoa API ...

  3. FFmpeg发送流媒体的命令(UDP,RTP,RTMP)

    http://blog.csdn.net/leixiaohua1020/article/details/38283297

  4. Json数据异步绑定到界面的Table并且自动刷新

    转自:http://blog.csdn.net/jianxin1009/article/details/8565828‘ 做Winform习惯了,大家都习惯设置datasource这样的写法. 如果想 ...

  5. (转载)Linux中cp直接覆盖不提示的方法

    (转载)http://soft.chinabyte.com/os/220/11760720.shtml 新做了服务器,cp覆盖时,无论加什么参数-f之类的还是提示是否覆盖,这在大量cp覆盖操作的时候是 ...

  6. RatingBar设置显示星星个数

    RatingBar评分控件 项目中遇到问题 marker一下: 关于自定义以及遇到的出现模糊情况 多半是因为切得图除颜色外 不一致的原因 如果大小也不一样,(沃日) 问题是这样的: 我可以通过OnRa ...

  7. HDU-2952 Counting Sheep (DFS)

    Counting Sheep Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Tota ...

  8. CMD删除Mysql 服务

    用sc.exe这个命令可以删除Windows系统服务 开始 —> 运行 —> cmd.exe,然后输入sc就可以看到了. 使用办法很简单: sc delete "服务名" ...

  9. python指定pypi的源地址 镜像地址

    python pip源介绍: python装包一般都用easy_install 或者pip. 他们俩的原理跟apt-get差不多,都是去某个地址去下载你所指定的包. pip和easy_install默 ...

  10. AIX项目总结_oracle_sqlloader_01

    近来一直在忙AIX的移行项目,但也因自己小小偷懒,所以到现在才开始记录.接下来,言归正传. 这个项目中,学习中了shell相关知识,从基本的语法命令(定义变量.特殊变量使用.循环控制.方法调用等)到l ...