题目描述

输入一个链表,输出该链表中倒数第k个结点。

时间限制:1秒;空间限制:32768K;本题知识点: 链表

解题思路

注意返回的是Node,而不是Node的Value。注意处理k超出范围的异常情况。

# -*- coding:utf-8 -*-
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def FindKthToTail(self, head, k):
        # write code here
        l=[]
        while head:
            l.append(head)
            head = head.next
        if len(l)<k or k<=0:
            return
        return l[-k]

 

相关文章:

  • 2021-04-23
  • 2021-12-12
  • 2021-08-13
  • 2021-05-26
  • 2021-06-30
  • 2022-12-23
  • 2022-12-23
  • 2022-01-09
猜你喜欢
  • 2021-05-02
  • 2021-10-31
  • 2022-02-08
  • 2021-07-23
  • 2022-12-23
相关资源
相似解决方案