Python:如何找出单链表中的倒数第K个元素

class LNode:
    def __init__(self):
        self.data = None
        self.next = None

def ConstructList():
    i = 1
    head = LNode()
    cur = head
    while i<8 :
        tmp = LNode()
        tmp.data = i
        cur.next = tmp
        cur = tmp
        i += 1
    return head

def PrintList(head):
    if head is None or head.next is None:
        print('Empty!')
        return
    cur = head.next
    while cur is not None:
        print(cur.data,end=' ')
        cur = cur.next

def FindLastK(head, k):
    if head == None or head.next == None :
        return head
    slow = LNode()
    fast = LNode()
    fast = head.next
    slow = head.next
    i = 0
    while i

运行结果

你可能感兴趣的:(Python,编程练习1)