链表中倒数第k个结点

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

python solution:

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

class Solution:
    def FindKthToTail(self, head, k):
        if head is None or k<1:
            return None
        work = head
        while k>1 and work.next is not None:
            k -= 1
            work = work.next
        if k>1:
            return None
        while work.next is not None:
            work = work.next
            head = head.next
        return head
原文地址:https://www.cnblogs.com/bernieloveslife/p/10423226.html