-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremove_linked_list_unsorted.py
83 lines (74 loc) · 2.04 KB
/
remove_linked_list_unsorted.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#remove duplicates from a unsorted linked list
class Node:
def __init__(self,data,next):
self.data = data
self.next = next
class LinkedList:
def __init__(self):
self.head = None
def append(self,data):
tmp = Node(data,None)
if self.head == None:
self.head = tmp
else:
temp = self.head
while temp.next is not None:
temp = temp.next
temp.next = tmp
def print(self):
t = self.head
while t is not None:
print(t.data)
t = t.next
def push(self,data):
tmp = Node(data,None)
tmp.next = self.head
self.head = tmp
def insert(self,index,element):
tmp = Node(element,None)
temp = self.head
while temp.data != index:
temp = temp.next
tmp.next = temp.next
temp.next = tmp
def length(self):
count = 0
temp =self.head
while temp:
count+=1
temp = temp.next
return count
def delete(self,datas):
temp = self.head
prev = None
if temp is not None:
if temp.data == datas:
self.head = temp.next
temp = None
return
while temp.data != datas:
prev = temp
temp = temp.next
prev.next = temp.next
temp = None
def removedup(self):
temp =self.head
s = set()
while temp is not None:
if temp.data in s:
self.delete(temp.data)
else:
s.add(temp.data)
temp = temp.next
if __name__ == '__main__':
LinkedLists = LinkedList()
LinkedLists.append(10)
LinkedLists.append(30)
LinkedLists.append(10)
LinkedLists.append(20)
LinkedLists.append(30)
LinkedLists.append(40)
LinkedLists.append(40)
LinkedLists.append(10)
LinkedLists.removedup()
LinkedLists.print()