Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e60bc30

Browse files
✨feat: Add 剑指 Offer 22
1 parent 0c0d18f commit e60bc30

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed

‎Index/链表.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@
1616
| [203. 移除链表元素](https://leetcode-cn.com/problems/remove-linked-list-elements/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/remove-linked-list-elements/solution/gong-shui-san-xie-yi-chu-lian-biao-yuan-ca6fu/) | 简单 | 🤩🤩🤩 |
1717
| [460. LFU 缓存](https://leetcode-cn.com/problems/lfu-cache/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/lfu-cache/solution/gong-shui-san-xie-yun-yong-tong-pai-xu-s-53m3/) | 困难 | 🤩🤩🤩🤩🤩 |
1818
| [1600. 皇位继承顺序](https://leetcode-cn.com/problems/throne-inheritance/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/throne-inheritance/solution/gong-shui-san-xie-shi-yong-dan-xiang-lia-7t65/) | 中等 | 🤩🤩🤩 |
19+
| [剑指 Offer 22. 链表中倒数第k个节点](https://leetcode-cn.com/problems/lian-biao-zhong-dao-shu-di-kge-jie-dian-lcof/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/lian-biao-zhong-dao-shu-di-kge-jie-dian-lcof/solution/gong-shui-san-xie-yi-ti-san-jie-zhan-dui-w3rz/) | 简单 | 🤩🤩🤩🤩🤩 |
1920
| [剑指 Offer 52. 两个链表的第一个公共节点](https://leetcode-cn.com/problems/liang-ge-lian-biao-de-di-yi-ge-gong-gong-jie-dian-lcof/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/liang-ge-lian-biao-de-di-yi-ge-gong-gong-jie-dian-lcof/solution/gong-shui-san-xie-zhao-liang-tiao-lian-b-ifqw/) | 简单 | 🤩🤩🤩🤩🤩 |
2021

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[剑指 Offer 22. 链表中倒数第k个节点](https://leetcode-cn.com/problems/lian-biao-zhong-dao-shu-di-kge-jie-dian-lcof/solution/gong-shui-san-xie-yi-ti-san-jie-zhan-dui-w3rz/)** ,难度为 **简单**
4+
5+
Tag : 「链表」、「栈」、「队列」、「快慢指针」
6+
7+
8+
9+
10+
输入一个链表,输出该链表中倒数第k个节点。为了符合大多数人的习惯,本题从1开始计数,即链表的尾节点是倒数第1个节点。
11+
12+
例如,一个链表有 6 个节点,从头节点开始,它们的值依次是 1、2、3、4、5、6。这个链表的倒数第 3 个节点是值为 4 的节点。
13+
14+
示例:
15+
```
16+
给定一个链表: 1->2->3->4->5, 和 k = 2.
17+
18+
返回链表 4->5.
19+
```
20+
21+
---
22+
23+
### 栈/队列 解法
24+
25+
一个使用额外空间的解法是利用栈(队列),将所有的节点压入占中栈(队列)中,令当前栈(队列)容量为 $cnt$。
26+
27+
然后从栈顶(队列头)弹出 $k$ 个($cnt - k + 1$ 个)元素,最后一个出栈(出队列)的元素即是答案。
28+
29+
![image.png](https://pic.leetcode-cn.com/1630544371-cFgLAj-image.png)
30+
31+
代码(栈):
32+
```Java
33+
class Solution {
34+
public ListNode getKthFromEnd(ListNode head, int k) {
35+
Deque<ListNode> d = new ArrayDeque<>();
36+
while (head != null) {
37+
d.addLast(head);
38+
head = head.next;
39+
}
40+
ListNode ans = null;
41+
while (k-- > 0) ans = d.pollLast();
42+
return ans;
43+
}
44+
}
45+
```
46+
代码(队列):
47+
```Java
48+
class Solution {
49+
public ListNode getKthFromEnd(ListNode head, int k) {
50+
Deque<ListNode> d = new ArrayDeque<>();
51+
while (head != null) {
52+
d.addLast(head);
53+
head = head.next;
54+
}
55+
k = d.size() - k + 1;
56+
ListNode ans = null;
57+
while (k-- > 0) ans = d.pollFirst();
58+
return ans;
59+
}
60+
}
61+
```
62+
* 时间复杂度:$O(n)$
63+
* 空间复杂度:$O(n)$
64+
65+
---
66+
67+
### 差值法
68+
69+
我们可以先对链表进行一次完整遍历,拿到总长度 $cnt,ドル最后由 $cnt - k$ 即是倒数第 $k$ 个节点距离 $head$ 节点的距离。
70+
71+
![image.png](https://pic.leetcode-cn.com/1630543998-jOEXKC-image.png)
72+
73+
代码:
74+
```Java
75+
class Solution {
76+
public ListNode getKthFromEnd(ListNode head, int k) {
77+
int cnt = 0;
78+
ListNode tmp = head;
79+
while (tmp != null && ++cnt > 0) tmp = tmp.next;
80+
cnt -= k;
81+
while (cnt-- > 0) head = head.next;
82+
return head;
83+
}
84+
}
85+
```
86+
* 时间复杂度:$O(n)$
87+
* 空间复杂度:$O(1)$
88+
89+
---
90+
91+
### 快慢指针
92+
93+
事实上,我们还可以使用「快慢指针」进行求解。
94+
95+
起始先让快指针 `fast` 先走 $k$ 步,此时 `fast``slow` 之间距离为 $k,ドル之后让 `fast``slow` 指针一起走(始终维持相对距离为 $k$),当 `fast` 到达链表尾部,`slow` 即停在倒数第 $k$ 个节点处。
96+
97+
![image.png](https://pic.leetcode-cn.com/1630544021-KRXyeD-image.png)
98+
99+
代码:
100+
```Java
101+
class Solution {
102+
public ListNode getKthFromEnd(ListNode head, int k) {
103+
ListNode slow = head, fast = head;
104+
while (k-- > 0) fast = fast.next;
105+
while (fast != null) {
106+
slow = slow.next;
107+
fast = fast.next;
108+
}
109+
return slow;
110+
}
111+
}
112+
```
113+
* 时间复杂度:$O(n)$
114+
* 空间复杂度:$O(1)$
115+
116+
---
117+
118+
### 最后
119+
120+
这是我们「刷穿 LeetCode」系列文章的第 `No.剑指 Offer 22. 链表中倒数第k个节点` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
121+
122+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
123+
124+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
125+
126+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
127+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /