[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个元素,LeetCode中相类似的题目有Kth Largest Element in an Array 数组中第k大的数字 和 Kth Smallest Element in a BST 二叉搜索树中的第K小的元素。但那两道题和这题又不一样,首先这道题是要在链表中操作,链表的特点就是不能通过下标来直接访问元素,而且要知道链表长度的话只能遍历,当然如果这道题先遍历一遍知道了长度,再来找倒数第k个就没啥挑战,也不是这道题考察的初衷。这道题可以用递归和非递归两种方法来解。我们先来看看递归的方法,方法是先无脑递归到最末尾,然后开始回去,每回一个计数器累加1,直到回到k返回节点即可,此方法的时间复杂度和空间复杂度均为O(n),代码如下:
解法一:
// Recursion
class Solution {
public:
ListNode *kthToLast(ListNode *head, int k) {
int i = ;
return kthToLastDFS(head, k, i);
}
ListNode *kthToLastDFS(ListNode *head, int k, int &i) {
if (!head) return head;
ListNode *node = kthToLastDFS(head->next, k, i);
++i;
if (i == k) return head;
return node;
}
};
下面我们来看非递归的方法,这种方法就巧妙的多,需要用两个指针,其中一个指针先向前走k个,然后两个指针同时走,当先走那个到达末尾时,另外一个指针指向的元素就是倒数第k个,时间复杂度为O(n),空间复杂度为O(1),代码如下:
解法二:
// Non-recursion
class Solution {
public:
ListNode *kthToLast(ListNode *head, int k) {
ListNode *pre = head, *cur = head;
for (int i = ; i < k; ++i) {
if (cur) cur = cur->next;
}
while (cur) {
cur = cur->next;
pre = pre->next;
}
return pre;
}
};
[CareerCup] 2.2 Kth to Last Element of Linked List 链表的倒数第k个元素的更多相关文章
- Leetcode703.Kth Largest Element in a Stream数据流中的第K大元素
设计一个找到数据流中第K大元素的类(class).注意是排序后的第K大元素,不是第K个不同的元素. 你的 KthLargest 类需要一个同时接收整数 k 和整数数组nums 的构造器,它包含数据流中 ...
- [Swift]LeetCode703. 数据流中的第K大元素 | Kth Largest Element in a Stream
Design a class to find the kth largest element in a stream. Note that it is the kth largest element ...
- [LeetCode] Kth Largest Element in a Stream 数据流中的第K大的元素
Design a class to find the kth largest element in a stream. Note that it is the kth largest element ...
- LeetCode OJ:Kth Smallest Element in a BST(二叉树中第k个最小的元素)
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...
- LeetCode 230 Kth Smallest Element in a BST 二叉搜索树中的第K个元素
1.非递归解法 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * ...
- 215 Kth Largest Element in an Array 数组中的第K个最大元素
在未排序的数组中找到第 k 个最大的元素.请注意,它是数组有序排列后的第 k 个最大元素,而不是第 k 个不同元素.例如,给出 [3,2,1,5,6,4] 和 k = 2,返回 5.注意事项:你可以假 ...
- 查找两个有序数组中的第K个元素(find kth smallest element in 2 sorted arrays)
查找两个有序数组中的第K个元素 int FindKth(int a[], int b[], int k, int astart, int aend, int bstart, int bend) { ; ...
- Leetcode 703题数据流中的第K大元素(Kth Largest Element in a Stream)Java语言求解
题目链接 https://leetcode-cn.com/problems/kth-largest-element-in-a-stream/ 题目内容 设计一个找到数据流中第K大元素的类(class) ...
- [LeetCode] Kth Smallest Number in Multiplication Table 乘法表中的第K小的数字
Nearly every one have used the Multiplication Table. But could you find out the k-th smallest number ...
随机推荐
- strcpy
/********************** *C语言标准库函数strcpy的一种典型的工业级的最简实现 *返回值:目标串的地址. *对于出现异常的情况ANSI-C99标准并未定义,故由实现者决定返 ...
- 【转载】主数据管理(MDM)与元数据管理
主数据(Master Data)和元数据(Meta Data)是两个完全不同的概念.元数据是指表示数据的相关信息,比如数据定义等,而主数据是指实例数据,比如产品目录信息等.比如,某省地税开发了一套 征 ...
- 深入探讨 Java 类加载器
转自:http://www.ibm.com/developerworks/cn/java/j-lo-classloader/ 类加载器(class loader)是 Java™中的一个很重要的概念.类 ...
- SQL2014内存表性能之内存中 OLTP 的性能改进测试
先贴1个例子,后续补充完整的测试例子.... 1.用MSDN例子测试一下 use master go --1.先创建包含内存优化文件组的数据库 CREATE DATABASE imoltp2 ON P ...
- MFC Grid control 2.27
原文链接地址:http://www.codeproject.com/Articles/8/MFC-Grid-control MFCGridCtrl是个强大的类,用于数据的表格显示. 1.类特征 Cel ...
- HDU 4421 Bit Magic (图论-2SAT)
Bit Magic Problem Description Yesterday, my teacher taught me about bit operators: and (&), or ( ...
- redis动态修改参数配置
./redis-cli -h 10.10.10.11 -p 6401 save # 保存当前快照 # 列出所有当前配置 config get * # 查看指定配置 config get ...
- 关于macOS Sierra无法使用gdb进行调试的解决方案
1.对gdb进行签名,签名过程详见:http://jingyan.baidu.com/article/d169e1864dc24d436611d839.html: 2.重新启动系统,同时按住键盘上的c ...
- MySQL常用技巧
1.查看MySQL版本信息: 1,登上 mysql>status; 2,登上 mysql>select version(); 3,登上 mysql> select @@version ...
- windows 7系统搭建本地SVN服务器的过程
Subversion是优秀的版本控制工具,其具体的的优点和详细介绍,这里就不再多说. 首先来下载和搭建SVN服务器. 现在Subversion已经迁移到apache网站上了,下载地址: http:// ...