链表中倒数第k个结点
Posted bernieloveslife
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了链表中倒数第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
以上是关于链表中倒数第k个结点的主要内容,如果未能解决你的问题,请参考以下文章