【剑指offer】面试题14—链表中倒数第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
        if head == None: return None
        p = head
        q = head
        while k-1:
            if p.next == None: return None
            p = p.next
            k = k-1
        while p.next:
            p = p.next
            q = q.next
        return q

你可能感兴趣的:(【剑指offer】面试题14—链表中倒数第k个结点)