以下是我到目前为止所做的代码,只是需要从链接列表中删除第一个和最后一个项目的两个小部分,我感到困惑。 (蟒)
class UnorderedList:
def __init__(self):
self.head = None
def __str__(self):
list1 = []
st1 = '['
curr = self.head
while curr != None:
list1.append(curr.get_data())
curr = curr.get_next()
list1.append(' ')
for x in range(0, len(list1) - 1):
st1 += str(list1[x])
st1 += ']'
return st1
def __iter__(self):
return LinkedListIterator(self.head)
def removefirst(self):
??
def removelast(self):
??
答案 0 :(得分:0)
在Python中使用snake_case而不是camelCase。
def remove_first(self):
temp = self.head
self.head = self.head.get_next()
del temp
def remove_last(self):
previous = None
current = self.head
while current.get_next() is not None:
previous = current
current = current.get_next()
previous.get_next() = None
del current