输入一个链表,输出该链表中倒数第k个结点。
快慢指针,判断好特殊情况:
- 空链表
- k大于链表的节点长度
class Solution:
def FindKthToTail(self , pHead , k ):
# write code here
if not pHead:
return None
fast,slow = pHead,pHead
while fast and k:
fast = fast.next
k -=1
if k > 0:
return None
while fast:
fast = fast.next
slow = slow.next
return slow