-
Notifications
You must be signed in to change notification settings - Fork 0
/
day-24-solution.py
76 lines (63 loc) · 1.4 KB
/
day-24-solution.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
# Data class
class Node:
def __init__(self, data):
self.next = None
self.data = data
# Custom List class
class MyList:
def insert(self, head, data):
p = Node(data)
if head == None:
# No nodes
head = p
elif head.next == None:
# Single node
head.next = p
else:
# More than 1 nodes
curr = head
while curr.next != None:
curr = curr.next
curr.next = p
return head
def display(self, head):
# Iterate over all nodes from head
curr = head
while curr:
print(curr.data, end=" ")
curr = curr.next
print()
def remove_duplicates(self, head):
if head == None:
# If no node
return head
# When more than 1 node
curr = head
while curr.next != None:
# Check for duplicates
if curr.data == curr.next.data:
curr.next = curr.next.next
else:
curr = curr.next
return head
if __name__ == "__main__":
# Read input integer from stdin
num_test_cases = int(input())
# Instantiate list class
my_list = MyList()
# Set `head` pointer to None
head = None
# Iteratively insert node
for i in range(num_test_cases):
# Read data input integer from stdin
data = int(input())
# Insert data as node
head = my_list.insert(head, data)
# Display list
print("List with duplicates:")
my_list.display(head)
# Remove duplicates from list
head = my_list.remove_duplicates(head)
# Display list again
print("List without duplicates:")
my_list.display(head)