首页 > 试题广场 >

链表中倒数最后k个结点

[编程题]链表中倒数最后k个结点
  • 热度指数:358587 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
输入一个长度为 n 的链表,设链表中的元素的值为 ai ,返回该链表中倒数第k个节点。
如果该链表长度小于k,请返回一个长度为 0 的链表。


数据范围:
要求:空间复杂度 ,时间复杂度
进阶:空间复杂度 ,时间复杂度

例如输入{1,2,3,4,5},2时,对应的链表结构如下图所示:
其中蓝色部分为该链表的最后2个结点,所以返回倒数第2个结点(也即结点值为4的结点)即可,系统会打印后面所有的节点来比较。
示例1

输入

{1,2,3,4,5},2

输出

{4,5}

说明

返回倒数第2个节点4,系统会打印后面所有的节点来比较。 
示例2

输入

{2},8

输出

{}

说明:本题目包含复杂数据结构ListNode,点此查看相关信息
class Solution:
    def FindKthToTail(self , pHead: ListNode, k: int) -> ListNode:
        # write code here
        tail = pHead
        kth = pHead
        for _ in range(k):
            if kth:
                kth = kth.next
            else:
                return None
        while kth:
            tail = tail.next
            kth = kth.next
        return tail

编辑于 2024-02-05 20:43:16 回复(0)
class Solution:
    def FindKthToTail(self , pHead: ListNode, k: int) -> ListNode:
        # write code here
        slow = pHead
        fast = pHead
        index = 0
        while fast != None:
            fast = fast.next
            index += 1
            if index > k:
                slow = slow.next
        if index < k:
            return None
        else:
            return slow

发表于 2023-08-17 09:04:36 回复(0)
#最简单的理解就是将链表转成数组,然后把后k个数据提出来,再组成链表
lass Solution:
    def FindKthToTail(self , pHead: ListNode, k: int) -> ListNode:
        # write code here
        num = []
        while pHead:
            num.append(pHead.val)
            pHead = pHead.next

        n = len(num)
        if n < k:
            return None
        numk = []
        for i in range(n-k,n):
            numk.append(num[i])
        new = ListNode(0)
        cur = new
        for m in numk:
            cur.next = ListNode(m)
            cur = cur.next
        return new.next

发表于 2023-05-30 16:36:00 回复(0)
空间复杂度O(1),时间复杂度O(n):
class Solution:
    def FindKthToTail(self , pHead: ListNode, k: int):
        pre = pHead
        suc = pHead
        for i in range(k):
            if suc:
                suc = suc.next
            else:
                return None
        while suc:
            pre = pre.next
            suc = suc.next
        else:
            return pre


发表于 2023-05-04 09:23:37 回复(0)
class Solution:
    def FindKthToTail(self , pHead: ListNode, k: int) -> ListNode:
        # write code here
        l = []
        while pHead:
            l.append(pHead)
            pHead = pHead.next 
        if len(l) < k or k == 0:
            return None 
        return l[-k]
发表于 2023-04-26 08:49:06 回复(0)
class Solution:
    def FindKthToTail(self , pHead: ListNode, k: int) -> ListNode:
        # write code here

        # 函数:求链表长度
        def lenth(pHead):
            n = 0
            while pHead:
                n += 1
                pHead = pHead.next
            return n

        # 判断长度是否小于k?
        l = lenth(pHead) 
        if l < k:
            return None
        else:
            n = l - k
            while n:
                pHead = pHead.next
                n -= 1
            return pHead
一个笨方法,只用一个指针
写一个函数:求链表长度
然后调用看是否小于k?
小于就输出none
如果大于的话,先求出长度和k的差值n
走n步,之后就是要输出的内容

发表于 2023-03-08 22:07:51 回复(0)
class Solution:
    def FindKthToTail(self , pHead: ListNode, k: int) -> ListNode:
        # write code here
        total_num = 0
        nh = ListNode(0)
        nh.next = pHead

        while pHead:
            pHead = pHead.next
            total_num += 1
        return_num = total_num - k
        if return_num<0:
            return None
        pHead = nh.next
        for i in range(return_num):
            pHead = pHead.next
        return pHead

发表于 2023-03-02 00:47:15 回复(0)
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param pHead ListNode类 
# @param k int整型 
# @return ListNode类
#
class Solution:
    def FindKthToTail(self , pHead: ListNode, k: int) -> ListNode:
        # write code here
        slow =  quick = pHead

        while quick and k != 0:
            quick = quick.next
            k -= 1
        
        while quick:
            slow = slow.next
            quick = quick.next

        return slow if k == 0 else None

发表于 2022-11-27 22:14:27 回复(0)
class Solution:
    def FindKthToTail(self , pHead: ListNode, kint) -> ListNode:
        # write code here
        head=pHead
        box=[]
        while head:
            box.append(head)
            head=head.next
        n=len(box)
        if n>=k and k!=0:
            res=box[-k]
        else:
            res=None
        return res
发表于 2022-11-13 12:21:12 回复(0)
class Solution:
    def FindKthToTail(self , pHead , k ):
        # write code here
        alist = []
        if pHead== None or k==0:
            return ListNode(0).next
        while(pHead!=None):
            alist.append(pHead)
            pHead = pHead.next
        n = len(alist)
        if n>=k:
            return alist[n-k]
        elif n==k:
            return alist[0]
        else :
            return ListNode(0).next
发表于 2022-03-18 17:50:20 回复(0)
class Solution:
    def FindKthToTail(self , pHead: ListNode, k: int) -> ListNode:
        fast, slow = pHead, pHead
        for _ in range(k):
            if not fast: return None
            fast = fast.next
        while fast:
            fast = fast.next
            slow = slow.next
        return slow

发表于 2021-11-17 21:15:12 回复(0)
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#

# @param pHead ListNode类 
# @param k int整型 
# @return ListNode类
#
class Solution:
    def FindKthToTail(self , pHead , k ):
        # write code here
        if pHead is None:
            return pHead
        len1 = 0
        p = pHead
        while pHead is not None:
            len1 = len1+1
            pHead = pHead.next
            
        if k > len1:
            return pHead
        elif k == len1:
            return p
        else:
            for i in range(len1-k):
                p = p.next
        return p
            
            
发表于 2021-08-04 23:22:21 回复(0)

问题信息

上传者:灵溪吴彦祖
难度:
14条回答 5802浏览

热门推荐

通过挑战的用户

查看代码