到目前为止,我已经想出了从普通列表创建链接列表的代码:def createList(plist):
linkedList = None
# goes backwards, adding each element to the beginning
# of the list.
for index in range(len(plist)-1, -1, -1):
linkedList = insertValueHead(linkedList, plist[index])
return linkedList
def insertValueHead(linkedList, value):
newnode = {}
newnode["data"] = value
#set the next pointer of this new node to the head of the list, linkedList
#newnode is now the head of the list
newnode["next"] = linkedList
return newnode
def listString(linkedList):
ptr = linkedList
str1 = ''
while ptr != None:
str1 += str(ptr['data'])
ptr = ptr['next']
if ptr != None:
str1 += "->"
str1 = str1
return str1
使用这段代码,我可以通过运行createList(plist)将一个普通列表(如[1,2,3,4])转换为这个列表:
^{pr2}$
现在我要做的是删除与链接列表中另一个节点相同的任何节点。所以如果我用一个列表运行这个程序,比如[1,1,2,5,7,7,8,8,10,10,10,10],它会返回1,2,5,7,8,10。我想知道如何从我正在创建的字典(链表)中删除重复的节点。到目前为止,这是我想出的代码,但我真的不知道从这里该怎么办:def noDups(plist):
node = plist
while node['next'] != None:
if node['data'] == node['next']['data']:
del node['next']
return node
为了测试这是我使用的函数:def testNoDups():
nums = createList([1,1,2,5,7,7,8,8,10,10,10,10,10])
print noDups(nums)
非常感谢任何帮助!:)