链表的两种创建方法(头插、尾插)Python实现
class Node(): def __init__(self, item): self.item = item self.next = None def create_linklist_head(li): head = Node(li[0]) for e in li[1:]: node = Node(e) node.next = head head = node return head def create_linklist_tail(li): head = Node(li[0]) tail = head for e in li[1:]: node = Node(e) tail.next = node tail = node return head def print_linklist(lk): while lk: print(lk.item, end=',') lk = lk.next if __name__ == '__main__': link_list_1 = create_linklist_head([12,11,10]) link_list_2 = create_linklist_tail([12,11,10]) print("头插法") print_linklist(link_list_1) print("\n尾插法") print_linklist(link_list_2)
展示:
时刻记着自己要成为什么样的人!