class Node(object):
def __init__(self, data, pointer):
self.data = data
self.next = pointer
# 创建单链表
class SingleLinkedList(object):
def __init__(self):
self.head = Node(None, None)
self.point = self.head
def append(self, data):
# 末尾追加节点
new_node = Node(data, None)
self.point.next = new_node
self.point = new_node
def insert(self, data, find):
# 插入数据(前向插入数据)
if not self.head.next:
print('链表为空')
return None
new_node = Node(data, None)
self.point = self.head
while self.point.next.data != find:
self.point = self.point.next
if self.point.next is None:
print('没有找到该元素')
return None
new_node.next = self.point.next
self.point.next = new_node
def delete(self, find):
# 删除节点
# 空链表
if not self.head.next:
print('链表为空')
return None
self.point = self.head
while self.point.next.data != find:
self.point = self.point.next
pointer = self.point.next
self.point.next = self.point.next.next
del pointer
def insert_after_head(self, data):
node = Node(data, None)
# bug 产生没写 if 返回
if not self.head.next:
self.head.next = node
return None
node.next = self.head.next
self.head.next = node
def reverse(self):
local_list = SingleLinkedList()
self.point = self.head
count = 0
while self.point.next:
count += 1
self.point = self.point.next
data = self.point.data
local_list.insert_after_head(data)
return local_list
def get_size(self):
count = 0
self.point = self.head
while self.point.next:
self.point = self.point.next
count += 1
retur