代码:
class Node:
def __init__(self,item):
self.item=item
self.next=Node
def creat_linklist_head(ls): #头插法
head=Node(ls)
for element in ls[1:]:
node=Node(element)
node.next=head
return head
def creat_linklist_tail(ls): #尾插法
head=Node(ls)
tail=head
for element in ls[1:]:
node=Node(element)
tail.next=node
tail=node
return head
def print_linklist(lk):
while lk:
print(lk.item,end=',')
lk=lk.next