1. 程式人生 > >劍指offer刷題之連結串列中倒數第k個結點

劍指offer刷題之連結串列中倒數第k個結點

題目描述

輸入一個連結串列,輸出該連結串列中倒數第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 != None:
            l.append(head)
            head = head.next
        if k>len(l) or k<1:
            return None
        else:
            return l[-k]