algorithm-base/animation-simulation/链表篇/剑指offer22倒数第k个节点.md

183 lines
5.7 KiB
Java
Raw Normal View History

2021-03-20 08:57:12 +00:00
> **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
>
>
>
> <u>[****](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
#### [ Offer 22. k](https://leetcode-cn.com/problems/lian-biao-zhong-dao-shu-di-kge-jie-dian-lcof/)
2021-03-19 07:07:33 +00:00
k11612345634
2021-07-15 16:06:52 +00:00
2021-03-19 07:07:33 +00:00
n
2021-07-15 16:06:52 +00:00
1038k
2021-03-19 07:07:33 +00:00
K-1K-1K
2021-03-20 04:46:42 +00:00
![](https://img-blog.csdnimg.cn/img_convert/506c4d70f4c50c66994711c8506462a8.gif)
2021-03-19 07:07:33 +00:00
2021-04-28 10:28:00 +00:00
****
Java Code:
2021-03-19 07:07:33 +00:00
```java
class Solution {
2021-03-20 04:46:42 +00:00
public ListNode getKthFromEnd (ListNode head, int k) {
2021-03-19 07:07:33 +00:00
//特殊情况
2021-03-20 04:46:42 +00:00
if (head == null) {
2021-03-19 07:07:33 +00:00
return head;
}
//初始化两个指针
ListNode pro = new ListNode(-1);
ListNode after = new ListNode(-1);
//定义指针指向
pro = head;
after = head;
//先移动绿指针到指定位置
2021-03-20 04:46:42 +00:00
for (int i = 0; i < k-1; i++) {
pro = pro.next;
2021-03-19 07:07:33 +00:00
}
//两个指针同时移动
while (pro.next != null) {
pro = pro.next;
after = after.next;
}
//返回倒数第k个节点
return after;
}
}
```
2021-04-28 10:28:00 +00:00
C++ Code:
```cpp
class Solution {
public:
ListNode * getKthFromEnd(ListNode * head, int k) {
//特殊情况
if (head == nullptr) {
return head;
}
//初始化两个指针
ListNode * pro = new ListNode(-1);
ListNode * after = new ListNode(-1);
//定义指针指向
pro = head;
after = head;
//先移动绿指针到指定位置
for (int i = 0; i < k-1; i++) {
pro = pro->next;
}
//两个指针同时移动
while (pro->next != nullptr) {
pro = pro->next;
after = after->next;
}
//返回倒数第k个节点
return after;
}
};
```
2021-05-04 20:26:39 +00:00
JS Code:
```javascript
var getKthFromEnd = function(head, k) {
2021-07-12 09:08:45 +00:00
//特殊情况
2021-05-04 20:26:39 +00:00
if(!head) return head;
2021-07-12 09:08:45 +00:00
//初始化两个指针, 定义指针指向
2021-05-04 20:26:39 +00:00
let pro = head, after = head;
2021-07-12 09:08:45 +00:00
//先移动绿指针到指定位置
2021-05-04 20:26:39 +00:00
for(let i = 0; i < k - 1; i++){
pro = pro.next;
}
2021-07-12 09:08:45 +00:00
//两个指针同时移动
2021-05-04 20:26:39 +00:00
while(pro.next){
pro = pro.next;
after = after.next;
}
2021-07-12 09:08:45 +00:00
//返回倒数第k个节点
2021-05-04 20:26:39 +00:00
return after;
};
2021-07-12 09:08:45 +00:00
```
Python Code:
```python
class Solution:
def getKthFromEnd(self, head: ListNode, k: int) -> ListNode:
#
if head is None:
return head
# ,
pro = head
after = head
# 绿
for _ in range(k - 1):
pro = pro.next
#
while pro.next is not None:
pro = pro.next
after = after.next
# k
return after
```
2021-07-17 14:28:06 +00:00
Swift Code
```swift
class Solution {
func getKthFromEnd(_ head: ListNode?, _ k: Int) -> ListNode? {
//特殊情况
if head == nil {
return head
}
//初始化两个指针
var pro = head, after = head
//先移动绿指针到指定位置
for i in 0..<k-1 {
pro = pro?.next
}
//两个指针同时移动
while pro?.next != nil {
pro = pro?.next
after = after?.next
}
//返回倒数第k个节点
return after
}
}
```
2021-07-25 15:20:13 +00:00
Go Code:
```go
func getKthFromEnd(head *ListNode, k int) *ListNode {
if head == nil { return head }
pro, after := head, head
//先移动绿指针到指定位置
for i := 0; i < k - 1; i++ {
pro = pro.Next
}
for pro.Next != nil {
pro = pro.Next
after = after.Next
}
return after
}
```