Skip to content

Commit 56aba20

Browse files
authored
Create linked_list.py
1 parent 78a3159 commit 56aba20

File tree

1 file changed

+179
-0
lines changed

1 file changed

+179
-0
lines changed

linked_list.py

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
# Create a Node class to create a node
2+
class Node:
3+
def __init__(self, data):
4+
self.data = data
5+
self.next = None
6+
7+
# Create a LinkedList class
8+
class LinkedList:
9+
def __init__(self):
10+
self.head = None
11+
12+
# Method to add a node at the beginning of the LL
13+
def insertAtBegin(self, data):
14+
new_node = Node(data)
15+
new_node.next = self.head
16+
self.head = new_node
17+
18+
# Method to add a node at any index
19+
# Indexing starts from 0.
20+
def insertAtIndex(self, data, index):
21+
if index == 0:
22+
self.insertAtBegin(data)
23+
return
24+
25+
position = 0
26+
current_node = self.head
27+
while current_node is not None and position + 1 != index:
28+
position += 1
29+
current_node = current_node.next
30+
31+
if current_node is not None:
32+
new_node = Node(data)
33+
new_node.next = current_node.next
34+
current_node.next = new_node
35+
else:
36+
print("Index not present")
37+
38+
# Method to add a node at the end of LL
39+
def insertAtEnd(self, data):
40+
new_node = Node(data)
41+
if self.head is None:
42+
self.head = new_node
43+
return
44+
45+
current_node = self.head
46+
while current_node.next:
47+
current_node = current_node.next
48+
49+
current_node.next = new_node
50+
51+
# Update node at a given position
52+
def updateNode(self, val, index):
53+
current_node = self.head
54+
position = 0
55+
while current_node is not None and position != index:
56+
position += 1
57+
current_node = current_node.next
58+
59+
if current_node is not None:
60+
current_node.data = val
61+
else:
62+
print("Index not present")
63+
64+
# Method to remove first node of linked list
65+
def remove_first_node(self):
66+
if self.head is None:
67+
return
68+
69+
self.head = self.head.next
70+
71+
# Method to remove last node of linked list
72+
def remove_last_node(self):
73+
if self.head is None:
74+
return
75+
76+
# If there's only one node
77+
if self.head.next is None:
78+
self.head = None
79+
return
80+
81+
# Traverse to the second last node
82+
current_node = self.head
83+
while current_node.next and current_node.next.next:
84+
current_node = current_node.next
85+
86+
current_node.next = None
87+
88+
# Method to remove a node at a given index
89+
def remove_at_index(self, index):
90+
if self.head is None:
91+
return
92+
93+
if index == 0:
94+
self.remove_first_node()
95+
return
96+
97+
current_node = self.head
98+
position = 0
99+
while current_node is not None and current_node.next is not None and position + 1 != index:
100+
position += 1
101+
current_node = current_node.next
102+
103+
if current_node is not None and current_node.next is not None:
104+
current_node.next = current_node.next.next
105+
else:
106+
print("Index not present")
107+
108+
# Method to remove a node from the linked list by its data
109+
def remove_node(self, data):
110+
current_node = self.head
111+
112+
# If the node to be removed is the head node
113+
if current_node is not None and current_node.data == data:
114+
self.remove_first_node()
115+
return
116+
117+
# Traverse and find the node with the matching data
118+
while current_node is not None and current_node.next is not None:
119+
if current_node.next.data == data:
120+
current_node.next = current_node.next.next
121+
return
122+
current_node = current_node.next
123+
124+
# If the data was not found
125+
print("Node with the given data not found")
126+
127+
# Print the size of the linked list
128+
def sizeOfLL(self):
129+
size = 0
130+
current_node = self.head
131+
while current_node:
132+
size += 1
133+
current_node = current_node.next
134+
return size
135+
136+
# Print the linked list
137+
def printLL(self):
138+
current_node = self.head
139+
while current_node:
140+
print(current_node.data)
141+
current_node = current_node.next
142+
143+
144+
# create a new linked list
145+
llist = LinkedList()
146+
147+
# add nodes to the linked list
148+
llist.insertAtEnd('a')
149+
llist.insertAtEnd('b')
150+
llist.insertAtBegin('c')
151+
llist.insertAtEnd('d')
152+
llist.insertAtIndex('g', 2)
153+
154+
# print the linked list
155+
print("Node Data:")
156+
llist.printLL()
157+
158+
# remove nodes from the linked list
159+
print("\nRemove First Node:")
160+
llist.remove_first_node()
161+
llist.printLL()
162+
163+
print("\nRemove Last Node:")
164+
llist.remove_last_node()
165+
llist.printLL()
166+
167+
print("\nRemove Node at Index 1:")
168+
llist.remove_at_index(1)
169+
llist.printLL()
170+
171+
# print the linked list after all removals
172+
print("\nLinked list after removing a node:")
173+
llist.printLL()
174+
175+
print("\nUpdate node Value at Index 0:")
176+
llist.updateNode('z', 0)
177+
llist.printLL()
178+
179+
print("\nSize of linked list:", llist.sizeOfLL())

0 commit comments

Comments
 (0)