#寻找链表倒数第k个元素(如head->1->2->3->4->5->6->7倒数第3个元素为5)
class Node:
def __init__(self,value,next):
self.value=value
self.next=next
class Solution:
def findlink(self,head,k):
if head==None or k<=0:
return None
slow=head
fast=head
for i in range(k-1): #让fast指针先走k-1步
if fast.next!=None:
fast=fast.next #此时fast在第k个位置
while fast.next!=None: #当fast走到末尾时,slow走到第k位置处
slow=slow.next
fast=fast.next
return slow
03-23
153