Skip to content

Commit a224717

Browse files
Added Linked List without maintaining a tail
1 parent e42330c commit a224717

File tree

3 files changed

+211
-105
lines changed

3 files changed

+211
-105
lines changed

DataStructures/LinkedList.py

Lines changed: 0 additions & 105 deletions
This file was deleted.
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
class Node:
2+
def __init__(self, data):
3+
self.data = data
4+
self.next = None
5+
6+
7+
class LinkedList:
8+
def __init__(self):
9+
self.head = None
10+
11+
def insert(self, data: int, index=None) -> None:
12+
"""
13+
Inserting a new element to the Linked List
14+
:param data: Data of the new element
15+
:param index: Index to insert
16+
:return: None
17+
18+
Time Complexity: O(n)
19+
"""
20+
21+
is_valid_index = isinstance(index, int)
22+
23+
if is_valid_index and index >= 0:
24+
self._insert_at_index(data, index)
25+
else:
26+
self._insert_at_tail(data)
27+
28+
def _insert_at_index(self, data: int, index: int) -> None:
29+
# If the list is empty
30+
if self.head is None:
31+
self.head = Node(data)
32+
return
33+
34+
# If the user wants to insert at the first position
35+
if index == 0:
36+
self._insert_at_head(data)
37+
else:
38+
new_node = Node(data)
39+
current_node = self.head
40+
previous_node = self.head
41+
42+
for i in range(index-1):
43+
# If the user has provided an index greater than the size of the list
44+
if current_node is None:
45+
previous_node.next = new_node
46+
return
47+
48+
previous_node = current_node
49+
current_node = current_node.next
50+
51+
new_node.next = current_node.next
52+
previous_node = current_node
53+
previous_node.next = new_node
54+
55+
def _insert_at_tail(self, data: int) -> None:
56+
"""
57+
Inserting an element at the end of the Linked List
58+
:param data: Data of the new node
59+
:return: None
60+
61+
Time Complexity: O(n)
62+
"""
63+
new_node = Node(data)
64+
current_node = self.head
65+
66+
# If there are no elements in the linked list
67+
if self.head is None:
68+
self.head = new_node
69+
return
70+
71+
# Traversing the elements of the list to find the last element
72+
while current_node.next is not None:
73+
current_node = current_node.next
74+
75+
current_node.next = new_node
76+
77+
def _insert_at_head(self, data) -> None:
78+
"""
79+
Inserts a new node at the head of the Linked List
80+
:param data: Data of the new node
81+
:return: None
82+
83+
Time Complexity: O(1)
84+
"""
85+
new_node = Node(data)
86+
prev_head = self.head
87+
88+
# Make the new node the head of the linked list and add link to old head
89+
new_node.next = prev_head
90+
self.head = new_node
91+
92+
def delete(self, index=None) -> None:
93+
"""
94+
Delete an element from the Linked List
95+
:param index: Index of the element to delete
96+
:return: None
97+
98+
Time Complexity: O(n)
99+
"""
100+
# If the list is empty, do not do anything
101+
if self.head is None:
102+
return
103+
104+
is_valid_index = isinstance(index, int)
105+
106+
if is_valid_index and index >= 0:
107+
self._delete_from_index(index)
108+
else:
109+
self._delete_from_tail()
110+
111+
def _delete_from_head(self) -> None:
112+
"""
113+
Delete an element from the head of the Linked List
114+
:return: None
115+
116+
Time Complexity: O(1)
117+
"""
118+
new_head = self.head.next
119+
self.head = new_head
120+
121+
def _delete_from_tail(self) -> None:
122+
"""
123+
Delete an element from the tail of the Linked List
124+
:return: None
125+
126+
Time Complexity: O(n)
127+
"""
128+
current_node = self.head
129+
previous_node = None
130+
131+
while current_node.next is not None:
132+
previous_node = current_node
133+
current_node = current_node.next
134+
135+
previous_node.next = None
136+
137+
def _delete_from_index(self, index: int) -> None:
138+
"""
139+
Delete an element from the the specified index of the Linked List
140+
:return: None
141+
142+
Time Complexity: O(n)
143+
"""
144+
if index == 0:
145+
self._delete_from_head()
146+
else:
147+
current_node = self.head
148+
previous_node = None
149+
150+
for i in range(index):
151+
# If the user has provided an index greater than the size of the list
152+
if current_node.next is None:
153+
previous_node.next = None
154+
return
155+
156+
previous_node = current_node
157+
current_node = current_node.next
158+
159+
previous_node.next = current_node.next
160+
current_node.next = None
161+
162+
def print_list(self) -> None:
163+
"""
164+
Prints the elements in the list
165+
166+
Time Complexity: O(n)
167+
:return: None
168+
"""
169+
current_node = self.head
170+
while current_node.next is not None:
171+
print(current_node.data, end=" -> ")
172+
current_node = current_node.next
173+
print(current_node.data)
174+
175+
176+
177+
178+
179+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from DataStructures.LinkedList.LinkedList import LinkedList
2+
3+
linked_list = LinkedList()
4+
linked_list2 = LinkedList()
5+
6+
# Inserting elements to the linked list
7+
linked_list.insert(0, 1) # linked_list.insert(data, index)
8+
linked_list.insert(10)
9+
linked_list.insert(20, 1000)
10+
linked_list.insert(30)
11+
linked_list.insert(5, 0)
12+
linked_list.insert(15, 2)
13+
linked_list.insert(40, 5000)
14+
linked_list.print_list()
15+
16+
# Inserting and Removing elements from the linked list
17+
linked_list2.delete()
18+
linked_list2.delete(-1000)
19+
linked_list2.delete(10)
20+
linked_list2.insert(0, 1) # linked_list.insert(data, index)
21+
linked_list2.insert(1, 1)
22+
linked_list2.insert(2, 100)
23+
linked_list2.insert(3)
24+
linked_list2.insert(4)
25+
linked_list2.insert(5)
26+
linked_list2.print_list()
27+
linked_list2.delete(3)
28+
linked_list2.print_list()
29+
linked_list2.delete(100) # linked_list.delete(index)
30+
linked_list2.print_list()
31+
linked_list2.delete(0)
32+
linked_list2.print_list()

0 commit comments

Comments
 (0)