|
| 1 | +"""Romove Dups! Write code to remove duplicates from an unsorted linked list. |
| 2 | +FOLLOW UP |
| 3 | +How would you solve this problem if a temporary |
| 4 | +buffer is not allowed? Hints: #9, #40 |
| 5 | +""" |
| 6 | + |
| 7 | +class Node: |
| 8 | + #Singly link list |
| 9 | + def __init__(self,data): |
| 10 | + self.data = data |
| 11 | + self.next = None |
| 12 | + |
| 13 | +class linklist: |
| 14 | + #linkList class |
| 15 | + def __init__(self): |
| 16 | + self.head = None |
| 17 | + |
| 18 | + def push(self,data): |
| 19 | + node = Node(data)# create a new node |
| 20 | + |
| 21 | + if self.head == None: #check to see if the head node is empty |
| 22 | + self.head = node # If its empty add it to the new node |
| 23 | + return |
| 24 | + #if the head of the linklist is filled |
| 25 | + |
| 26 | + current = self.head |
| 27 | + while current.next is not None:#Check the current postion is empty |
| 28 | + #Move to the next line if nothing is there |
| 29 | + current = current.next |
| 30 | + |
| 31 | + current.next = node #point self.head to a new node |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | + def lenght(self): |
| 37 | + #note the count doesn't start at zero |
| 38 | + cur = self.head |
| 39 | + counter = 0 |
| 40 | + while cur is not None: |
| 41 | + counter+=1 |
| 42 | + cur = cur.next |
| 43 | + print('Linklist len: '+str(counter)) |
| 44 | + |
| 45 | + def printList(self): |
| 46 | + curr = self.head |
| 47 | + elem = [] |
| 48 | + |
| 49 | + while(curr != None): |
| 50 | + elem.append(curr.data) |
| 51 | + curr = curr.next |
| 52 | + print(elem) |
| 53 | + #1->2->3 |
| 54 | + def remove_node(self,data): |
| 55 | + #1->2->3 |
| 56 | + curr = self.head |
| 57 | + if curr is not None and curr.data == data: |
| 58 | + self.head = curr.next |
| 59 | + curr = None |
| 60 | + #look for the node that has the data we are looking for |
| 61 | + while curr is not None: |
| 62 | + if curr.data == data: |
| 63 | + break |
| 64 | + prev = curr |
| 65 | + curr = curr.next |
| 66 | + |
| 67 | + #if data isn't found just reutrn |
| 68 | + if(curr == None): |
| 69 | + return |
| 70 | + |
| 71 | + #allow us to unlink the nodes |
| 72 | + prev.next = curr.next |
| 73 | + curr = None |
| 74 | + |
| 75 | + def remove_dups(self): |
| 76 | + d_ = set()# create a set where only unquine element can be in |
| 77 | + curr = self.head |
| 78 | + while curr is not None: |
| 79 | + d_.add(curr.data)#add all the elements to the set |
| 80 | + curr = curr.next |
| 81 | + d_ = list(d_)#make theem in to a list |
| 82 | + self.head = None#clear the orignal link list |
| 83 | + for i in range(len(d_)): |
| 84 | + self.push(d_[i])#Use the element in the set a build a linklist with no dups |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | +#Testing to see if my class works |
| 92 | +llist = linklist() |
| 93 | +llist.push(2) |
| 94 | +llist.push(2) |
| 95 | +llist.push(2) |
| 96 | +llist.push(2) |
| 97 | +llist.push(2) |
| 98 | +llist.printList() |
| 99 | +llist.lenght() |
| 100 | +#Testing 1->2->1->3->1 result should only be 1->2->3 |
| 101 | +llist.remove_dups() |
| 102 | +llist.printList() |
| 103 | +llist.lenght() |
0 commit comments