本问题对应的 leetcode 原文链接:剑指 Offer 22. 链表中倒数第k个节点
问题描述
输入一个链表,输出该链表中倒数第k个节点。为了符合大多数人的习惯,本题从1开始计数,即链表的尾节点是倒数第1个节点。
例如,一个链表有 6 个节点,从头节点开始,它们的值依次是 1、2、3、4、5、6。这个链表的倒数第 3 个节点是值为 4 的节点。
示例:
给定一个链表: 1->2->3->4->5, 和 k = 2.
返回链表 4->5.
代码实现
class Solution {
public ListNode getKthFromEnd(ListNode head, int k) {
if(head == null){
return null;
}
ListNode fast = head, slow = head;
for(int i = 0; i < k; i++){
if(fast == null){
return null;
}
fast = fast.next;
}
while(fast != null){
fast = fast.next;
slow = slow.next;
}
return slow;
}
}
时间复杂度:O(n)
空间复杂度:O(1)
Python
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def getKthFromEnd(self, head, k):
"""
:type head: ListNode
:type k: int
:rtype: ListNode
"""
if head is None:
return None
fast = slow = head
for i in range(k):
if fast is None:
return None
fast = fast.next
while fast is not None:
fast = fast.next
slow = slow.next
return slow
C++
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* getKthFromEnd(ListNode* head, int k) {
if (head == nullptr) {
return nullptr;
}
ListNode* fast = head;
ListNode* slow = head;
for (int i = 0; i < k; i++) {
if (fast == nullptr) {
return nullptr;
}
fast = fast->next;
}
while (fast != nullptr) {
fast = fast->next;
slow = slow->next;
}
return slow;
}
};
Go
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func getKthFromEnd(head *ListNode, k int) *ListNode {
if head == nil {
return nil
}
fast := head
slow := head
for i := 0; i < k; i++ {
if fast == nil {
return nil
}
fast = fast.Next
}
for fast != nil {
fast = fast.Next
slow = slow.Next
}
return slow
}
JS
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @param {number} k
* @return {ListNode}
*/
var getKthFromEnd = function(head, k) {
if (head == null) {
return null;
}
let fast = head,
slow = head;
for (let i = 0; i < k; i++) {
if (fast == null) {
return null;
}
fast = fast.next;
}
while (fast != null) {
fast = fast.next;
slow = slow.next;
}
return slow;
};
评论(13)
快慢指针方法
”’cpp
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode getKthFromEnd(ListNode* head, int k) {
if(headnullptr) return head;
ListNode* slow=head;
ListNode* fast=head;
for(int i =0;i<k;i++){
fast=fast->next;
}
while(fast!=NULL)
{
fast=fast->next;
slow=slow->next;
}
return slow;
}
};
”’
空间O(1)时间O(n)
思路:fast 比 slow 多走 k 步,然后 slow 和 fast 一起走,当 fast 等于 null 时,slow 正好是倒数第 k 个节点
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode getKthFromEnd(ListNode* head, int k) {
/* 使用快慢指针,慢指针从头节点开始,快指针从
从第k个节点开始。当快指针遍历到队尾时,慢指针指向倒数第k个节点 */
ListNode *slow = head, *fast = head;
while(fast != NULL && k–)
{
fast = fast->next;
}
};
解法:快慢指针
思路:fast 比 slow 多走 k 步,然后 slow 和 fast 一起走,当 fast null 时,slow 正好是倒数第 k 个节点
时间复杂度:O(n)
空间复杂度:O(1)
时间复杂度O(n)
空间复杂度O(1)
时间复杂度O(N) 链表长度为n则遍历了n-k次
空间复杂度O(1) 使用了2个变量
额