剑指offer python版 链表中倒数第k个结点

class ListNode(object):
    def __init__(self,x):
        self.val=x
        self.next=None
        
class Link(object):
    def __init__(self,values=None):
        self.nodes=self.set_link(values) if  values else None
        
    def get_link(self):
        return self.nodes
    def set_link(self,values):
        if not values :
            return False
        head=ListNode(0)
        move=head
        
        
        try:
            for i in values:
                tmp=ListNode(i)
                move.next=tmp
                move=move.next
                
        except Exception as e:
            print(e)
            
        return head.next                
                
def aa(head,k):
    if not head:
        return False
    move=head
    while k>0:
        move=move.next
        k -=1
    while move:
        head=head.next
        move=move.next
        
    return head.val


a=Link([1,2,3,3,4,5,6,7])

b=a.get_link()

print(aa(b,3))

 

posted @ 2018-10-25 16:52  findtruth123  阅读(166)  评论(0编辑  收藏  举报