“”"
初始化链表
“”"
import time
class Node:
def init(self,value):
self.value = value
self.next = None
class SingleLinkList:
def init(self):
self.head = None
def is_empty(self):
"""判断是否为空"""
return self.head == None
def travel(self):
"""遍历链表"""
cur = self.head
while cur is not None:
print(cur.value,end=" ")
cur = cur.next
def append(self,item):
"""在末尾添加节点"""
node = Node(item)
# 如果是空链表
if self.is_empty():
self.head = node
return
# 当不为空的时候
cur = self.head
while cur.next:
cur = cur.next
cur.next = node
# node.next = None
def insert(self,index,item):
"""插入中间节点"""
if (self.head is None) and (self.head.next is None