[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 ...
随机推荐
- Swift面向对象基础(上)——Swift中的枚举
Swift中枚举 学习笔记来自<极客学院> import Foundation /**********1*Swift定义枚举的语法格式*************/ /* enum 枚举名 ...
- .net串口通信
背景: 前一段时间需要写一个向蓝牙模块发消息的功能. 对蓝牙的机制不太了解,所以一直在查资料, 但始终没找到我需要的东西,还误以为需要配套的一套开发模板和开发包, 偶然间发现只需要简单的串口通信,并且 ...
- 关于Math类的round、floor、ceil三个方法
一.Math类这三个方法的简介 1.round():取最接近的值. 对于这个方法,查看源代码,其实现如下: public static long round(double a) { if (a != ...
- linux编译安装MySQL
最近就想搞搞hadoop,然后装个MySQL,测试一下那个sqoop. MySQL这种东西,既然是开放源码的,那就源码安装吧. 下面是我的测试环境说明: VMware10+Ubuntu14.04 Ky ...
- 如此低价的ZBrush,你能想象?
作为3D艺术的狂热者,你是否曾为找不到一款适合自己的雕刻软件而苦恼?要么,你已经找到了,却因为昂贵的价格而迟迟不肯入手? 作为改变整个三维行业的业界先进的数字雕刻和绘画软件,ZBrush向来拥有广 ...
- 边工作边刷题:70天一遍leetcode: day 72
Missing Range 要点:题简单,这类题的特点都是记录上一步的状态,比如这题是end 错误点: 三种情况:一是连续的,即和上一个end差1,而是中间只差1个数,没有'->',最后是大于1 ...
- UESTC 1237 质因子分解
水题一枚.. #include<iostream> #include<cstdio> #include<cstring> #include<cmath> ...
- C++中不能声明为虚函数的有哪些函数
常见的不不能声明为虚函数的有:普通函数(非成员函数):静态成员函数:内联成员函数:构造函数:友元函数. 1.为什么C++不支持普通函数为虚函数? 普通函数(非成员函数)只能被overload,不能被o ...
- .NET深入 c#数据类型2
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- f2fs解析(八)node 管理器中的node_info
free_info 功成身退,node_info顺利接班. // 这里还是蛮复杂的一件事,如果不搞清除的话,这个历史性的接班工作我们就接不上 上面说到 alloc_nid 和 alloc_nid_do ...