-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
/
Copy pathremove_duplicates.py
159 lines (128 loc) · 3.61 KB
/
remove_duplicates.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
class Node:
def __init__(self, data: int = None):
self.data = data
self.next = None
def __repr__(self):
"""Returns a visual representation of the node and all its following nodes."""
string_rep = ""
temp = self
while temp:
string_rep += f"<{temp.data}> ---> "
temp = temp.next
string_rep += "<END>"
return string_rep
def create(arr: list[int]) -> Node:
"""
Creates a linked list from an array and returns the head of the linked list.
Args:
arr (list[int]): The array of integers.
Returns:
Node: The head of the linked list.
Example:
>>> arr = [1, 3, 5, 7, 9, 11, 11, 15, 17, 19]
>>> head = create(arr)
>>> head.data
1
>>> head.next.data
3
"""
if not arr:
raise ValueError("The array is empty")
head = Node(arr[0])
current = head
for data in arr[1:]:
current.next = Node(data)
current = current.next
return head
def display(head: Node) -> str:
"""
Display the elements of the linked list.
Args:
head (Node): The head of the linked list.
Returns:
str: A visual representation of the linked list.
Example:
>>> arr = [1, 3, 5, 7, 9]
>>> head = create(arr)
>>> display(head)
'<1> ---> <3> ---> <5> ---> <7> ---> <9> ---> <END>'
"""
result = ""
current = head
while current:
result += f"<{current.data}> ---> "
current = current.next
result += "<END>"
return result
def count(head: Node) -> int:
"""
Count the number of nodes in the linked list.
Args:
head (Node): The head of the linked list.
Returns:
int: The count of nodes in the linked list.
Example:
>>> arr = [1, 3, 5, 7, 9]
>>> head = create(arr)
>>> count(head)
5
"""
count = 0
current = head
while current:
count += 1
current = current.next
return count
def delete(head: Node, index: int) -> int:
"""
Delete a node at a given index from the linked list and return its value.
Args:
head (Node): The head of the linked list.
index (int): The index at which the node should be deleted (1-based index).
Returns:
int: The value of the deleted node.
Example:
>>> arr = [1, 3, 5, 7, 9]
>>> head = create(arr)
>>> delete(head, 3)
5
>>> display(head)
'<1> ---> <3> ---> <7> ---> <9> ---> <END>'
"""
if index < 1 or index > count(head):
return -1
if index == 1:
value = head.data
head = head.next
return value
else:
current = head
for _ in range(index - 2):
current = current.next
value = current.next.data
current.next = current.next.next
return value
def remove_duplicates(head: Node):
"""
Remove duplicates from a sorted linked list.
Args:
head (Node): The head of the sorted linked list.
Example:
>>> arr = [1, 3, 3, 5, 5, 7]
>>> head = create(arr)
>>> remove_duplicates(head)
>>> display(head)
'<1> ---> <3> ---> <5> ---> <7> ---> <END>'
"""
current = head
while current and current.next:
if current.data == current.next.data:
current.next = current.next.next
else:
current = current.next
if __name__ == "__main__":
arr = [1, 3, 5, 7, 9, 11, 11, 15, 17, 19]
head = create(arr)
remove_duplicates(head)
print("Linked List:")
print(display(head))