How to delete a node in a linked list in python? -
How to delete a node in a linked list in python? -
so far i've come code create linked list normal list:
def createlist(plist): linkedlist = none # goes backwards, adding each element origin # of list. index in range(len(plist)-1, -1, -1): linkedlist = insertvaluehead(linkedlist, plist[index]) homecoming linkedlist def insertvaluehead(linkedlist, value): newnode = {} newnode["data"] = value #set next pointer of new node head of list, linkedlist #newnode head of list newnode["next"] = linkedlist homecoming newnode def liststring(linkedlist): ptr = linkedlist str1 = '' while ptr != none: str1 += str(ptr['data']) ptr = ptr['next'] if ptr != none: str1 += "->" str1 = str1 homecoming str1
using code able turn normal list such [1,2,3,4] instead running createlist(plist):
{'data': 1, 'next': {'data': 2, 'next': {'data': 3, 'next': {'data': 4, 'next': none}}}}
right i'm trying delete node same node in linked list. if run programme list such [1,1,2,5,7,7,8,8,10,10,10,10,10] homecoming 1,2,5,7,8,10. wondering how go deleting duplicate nodes dictionary (linked list) creating. far code i've come don't know go here:
def nodups(plist): node = plist while node['next'] != none: if node['data'] == node['next']['data']: del node['next'] homecoming node
and test function using:
def testnodups(): nums = createlist([1,1,2,5,7,7,8,8,10,10,10,10,10]) print nodups(nums)
any help appreciated! :)
just create set out of list, convert list, , duplicates eliminated.
def createlist(plist): linkedlist = none # goes backwards, adding each element origin # of list. plist = list(set(plist)) index in range(len(plist)-1, -1, -1): linkedlist = insertvaluehead(linkedlist, plist[index]) homecoming linkedlist
this eliminate duplicates plist before index in range iteration , plist retain unique values. you're looking or trying eliminate consecutive duplicate values?
python linked-list
Comments
Post a Comment