牛客网剑指offer-输出链表的导数第k个节点

# class Solution:
#     def duplicate(self , numbers ):
#         # write code here
#         ls = {}
#         for i in numbers:
#             if i in ls:
#                 ls[i] += 1
#             else:
#                 ls[i] = 1
#         for k in numbers:
#             if ls[k] >= 2:
#                 return k
#         return -1

#这个是我写的版本====一定要写上循环呀!!!!!!。。。。。
class Solution:
    def FindKthToTail(self , pHead , k ):
        pur,outpoint = pHead,pHead
        length,count = 0,0
        while pur:
            pur = pur.next
            length += 1
        if k > length:
            return None
        while pHead:
            if count < (length - k):
                outpoint = outpoint.next
                count += 1
            else:
                return outpoint
            
        
#这个代码不懂
class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None
class Solution:
    def FindKthToTail(self , pHead , k ):
        if not pHead:
            return pHead
        if k <= 0:
            node = ListNode(0)
            return node.next
        last_node = pHead
        for i in range(k - 1):
            if last_node.next != None:
                last_node = last_node.next
            else:
                return last_node.next
        while last_node.next != None:
            last_node = last_node.next
            pHead = pHead.next
        return pHead

#这是通过的案例#
class Solution:
    def FindKthToTail(self , pHead , k ):
        pur,outpoint = pHead,pHead
        length,count = 0,0
        while pur:
            pur = pur.next
            length += 1
         
        while pHead:
            if count == length - k :
                return pHead
            pHead = pHead.next
            count += 1
#这是我原来的版本
class Solution:
    def FindKthToTail(self , pHead , k ):
        pur,outpoint = pHead,pHead
        length,count = 0,0
        if pur != None:
            pur = pur.next
            length += 1
        if count < (length - k + 1):
            pHead = pHead.next
            count += 1
        return pHead
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

十子木

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值