comments | difficulty | edit_url |
---|---|---|
true |
简单 |
输入一个链表,输出该链表中倒数第k个节点。为了符合大多数人的习惯,本题从1开始计数,即链表的尾节点是倒数第1个节点。
例如,一个链表有 6
个节点,从头节点开始,它们的值依次是 1、2、3、4、5、6
。这个链表的倒数第 3
个节点是值为 4
的节点。
示例:
给定一个链表: 1->2->3->4->5, 和 k = 2. 返回链表 4->5.
我们可以定义快慢指针 fast
和 slow
,初始时均指向 head
。
然后快指针 fast
先向前走
时间复杂度
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def getKthFromEnd(self, head: ListNode, k: int) -> ListNode:
slow = fast = head
for _ in range(k):
fast = fast.next
while fast:
slow, fast = slow.next, fast.next
return slow
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode getKthFromEnd(ListNode head, int k) {
ListNode slow = head, fast = head;
while (k-- > 0) {
fast = fast.next;
}
while (fast != null) {
slow = slow.next;
fast = fast.next;
}
return slow;
}
}
/**
* 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) {
ListNode *slow = head, *fast = head;
while (k--) {
fast = fast->next;
}
while (fast) {
slow = slow->next;
fast = fast->next;
}
return slow;
}
};
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func getKthFromEnd(head *ListNode, k int) *ListNode {
slow, fast := head, head
for ; k > 0; k-- {
fast = fast.Next
}
for fast != nil {
slow, fast = slow.Next, fast.Next
}
return slow
}
// Definition for singly-linked list.
// #[derive(PartialEq, Eq, Clone, Debug)]
// pub struct ListNode {
// pub val: i32,
// pub next: Option<Box<ListNode>>
// }
//
// impl ListNode {
// #[inline]
// fn new(val: i32) -> Self {
// ListNode {
// next: None,
// val
// }
// }
// }
impl Solution {
pub fn get_kth_from_end(head: Option<Box<ListNode>>, k: i32) -> Option<Box<ListNode>> {
let mut fast = &head;
for _ in 0..k {
fast = &fast.as_ref().unwrap().next;
}
let mut slow = &head;
while let (Some(nf), Some(ns)) = (fast, slow) {
fast = &nf.next;
slow = &ns.next;
}
slow.to_owned()
}
}
/**
* 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) {
let fast = head;
while (k--) {
fast = fast.next;
}
let slow = head;
while (fast) {
slow = slow.next;
fast = fast.next;
}
return slow;
};
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode GetKthFromEnd(ListNode head, int k) {
ListNode fast = head, slow = head;
while (k-- > 0) {
fast = fast.next;
}
while (fast != null) {
slow = slow.next;
fast = fast.next;
}
return slow;
}
}
/* public class ListNode {
* var val: Int
* var next: ListNode?
* init(_ val: Int) {
* self.val = val
* self.next = nil
* }
* }
*/
class Solution {
func getKthFromEnd(_ head: ListNode?, _ k: Int) -> ListNode? {
var slow = head
var fast = head
var k = k
while k > 0 {
fast = fast?.next
k -= 1
}
while fast != nil {
slow = slow?.next
fast = fast?.next
}
return slow
}
}