【发布时间】:2020-08-29 03:34:24
【问题描述】:
第一次发帖。我正在尝试使用下面的代码实现链表,但不知何故,链表会在第二个节点之后停止。我期待 9->6->11->8->15->19->7->,但我只得到了 9->6->。 谁能帮我弄清楚我的代码有什么问题? 谢谢!
class Node:
def __init__(self,value):
self.next = None
self.val = value
def __str__(self):
return str(self.val)
class SLinkedList:
def __init__(self):
self.head = None
self.tail = None
def append_node(self,value):
if self.head == None:
self.head = self.tail = Node(value)
else:
self.tail.next = Node(value)
self.tail = Node(value)
return self
llist = SLinkedList()
llist.append_node(9).append_node(6).append_node(11) \
.append_node(8).append_node(15).append_node(19) \
.append_node(7)
print(llist.head.next.next) # returned None. Why??
【问题讨论】:
标签: python linked-list singly-linked-list