mirror of
https://github.com/CyC2018/Interview-Notebook.git
synced 2026-10-30 17:29:20 +00:00
Update 22. 链表中倒数第 K 个结点.md
题目链接改为力扣中的对应题目,题目描述和代码按该题的接口调整。
This commit is contained in:
+11
-11
@@ -1,6 +1,6 @@
|
||||
# 22. 链表中倒数第 K 个结点
|
||||
|
||||
[牛客网](https://www.nowcoder.com/practice/886370fe658f41b498d40fb34ae76ff9?tpId=13&tqId=11167&tab=answerKey&from=cyc_github)
|
||||
[Leetcode](https://leetcode.cn/problems/lian-biao-zhong-dao-shu-di-kge-jie-dian-lcof/)
|
||||
|
||||
## 解题思路
|
||||
|
||||
@@ -9,19 +9,19 @@
|
||||
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/6b504f1f-bf76-4aab-a146-a9c7a58c2029.png" width="500"/> </div><br>
|
||||
|
||||
```java
|
||||
public ListNode FindKthToTail(ListNode head, int k) {
|
||||
public ListNode trainingPlan(ListNode head, int cnt) {
|
||||
if (head == null)
|
||||
return null;
|
||||
ListNode P1 = head;
|
||||
while (P1 != null && k-- > 0)
|
||||
P1 = P1.next;
|
||||
if (k > 0)
|
||||
ListNode p1 = head;
|
||||
while (p1 != null && cnt-- > 0)
|
||||
p1 = p1.next;
|
||||
if (cnt > 0)
|
||||
return null;
|
||||
ListNode P2 = head;
|
||||
while (P1 != null) {
|
||||
P1 = P1.next;
|
||||
P2 = P2.next;
|
||||
ListNode p2 = head;
|
||||
while (p1 != null) {
|
||||
p1 = p1.next;
|
||||
p2 = p2.next;
|
||||
}
|
||||
return P2;
|
||||
return p2;
|
||||
}
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user