題目描述
輸入一個(gè)鏈表辛掠,輸出該鏈表中倒數(shù)第k個(gè)結(jié)點(diǎn)。
示例1
輸入
1,{1,2,3,4,5}
返回值
{5}
解題思路:快慢指針,快的先走指定的步數(shù),慢的再走
# -*- 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
? ? ? ? fast,slow = head, head
? ? ? ? while fast and k > 0:
? ? ? ? ? ? fast = fast.next
? ? ? ? ? ? k -= 1
? ? ? ? if k > 0:
? ? ? ? ? ? return None
? ? ? ? while fast:
? ? ? ? ? ? fast = fast.next
? ? ? ? ? ? slow = slow.next
? ? ? ? return slow
原文鏈接:金烏智能 -鏈表中倒數(shù)第k個(gè)結(jié)點(diǎn)?轉(zhuǎn)載請(qǐng)聯(lián)系:金烏智能--數(shù)據(jù)抓取野崇、數(shù)據(jù)采集、爬蟲