Skip to content

Commit 0fbf269

Browse files
committed
Adding linked list implementation
1 parent 4619510 commit 0fbf269

File tree

1 file changed

+154
-0
lines changed

1 file changed

+154
-0
lines changed

data_structures/singly_linked_list.py

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
# Singly Linked List (a.k.a Linked List) Implementation
2+
3+
class Node:
4+
"""Node implementation.
5+
"""
6+
7+
def __init__(self, value) -> None:
8+
"""Class constructor.
9+
10+
Args:
11+
value (_type_): Value associated with each node.
12+
"""
13+
self.value = value
14+
self.next = None
15+
16+
17+
class SinglyLinkedList:
18+
"""Singly linked list implementation.
19+
"""
20+
21+
def __init__(self) -> None:
22+
"""Class constructor.
23+
"""
24+
self.head = None
25+
26+
def insert_at_head(self, value) -> None:
27+
"""Method to insert node at the head of the linked list.
28+
29+
Args:
30+
value (_type_): Value to be inserted at the head of the linked list.
31+
"""
32+
if self.head is None:
33+
self.head = new_node
34+
return
35+
36+
new_node = Node(value=value)
37+
new_node.next = self.head
38+
self.head = new_node
39+
40+
def insert_at_tail(self, value) -> None:
41+
"""Method to insert node at the tail of the linked list.
42+
43+
Args:
44+
value (_type_): Value to be inserted at the tail of the linked list.
45+
"""
46+
if self.head is None:
47+
self.insert_at_head(value=value)
48+
return
49+
50+
curr = self.head
51+
while curr.next is not None:
52+
curr = curr.next
53+
54+
curr.next = Node(value=value)
55+
56+
def insert_at_position(self, value, position) -> None:
57+
"""Method to insert aa node with associated value at a `position`.
58+
59+
Args:
60+
value (_type_): Value to be inserted in the linked list.
61+
position (_type_): Position (positive integer) at which a value is to be inserted.
62+
63+
Raises:
64+
ValueError: If `position` is greater than the actual size of the linked list.
65+
"""
66+
assert position >= 0
67+
68+
if self.get_size() < position:
69+
raise ValueError("Invalid position.")
70+
71+
if position == 0:
72+
self.insert_at_head(value=value)
73+
return
74+
75+
curr = self.head
76+
count = 0
77+
while count != position-1:
78+
curr = curr.next
79+
count += 1
80+
81+
new_node = Node(value)
82+
new_node.next = curr.next
83+
curr.next = new_node
84+
85+
def delete_at_position(self, position: int) -> None:
86+
"""Method to delete value at `position` from the linked list.
87+
88+
Args:
89+
position (int): Position (postive integer) for which value has to be deleted.
90+
91+
Raises:
92+
ValueError: If `position` is greater than the actual size of the linked list or empty linked list.
93+
"""
94+
assert position >= 0
95+
96+
if self.get_size() < position or self.is_empty():
97+
raise ValueError("Invalid position. Cannot delete.")
98+
99+
if position == 0:
100+
self.head = self.head.next
101+
return
102+
103+
count = 0
104+
curr = self.head
105+
while count != position-1:
106+
curr = curr.next
107+
count += 1
108+
109+
curr.next = curr.next.next
110+
111+
def reverse(self) -> None:
112+
"""Method to reverse the linked list.
113+
"""
114+
if self.is_empty():
115+
return 0
116+
117+
prev = None
118+
curr = self.head
119+
120+
while curr is not None:
121+
next_node = curr.next
122+
curr.next = prev
123+
prev = curr
124+
curr = next_node
125+
126+
self.head = prev
127+
128+
def is_empty(self) -> bool:
129+
"""Method to check if the linked list is empty.
130+
131+
Returns:
132+
bool: Returns a boolean `True` if empty, `False` otherwise.
133+
"""
134+
if self.head is None:
135+
return True
136+
137+
return False
138+
139+
def get_size(self) -> int:
140+
"""Method to count the number of nodes in the linked list/
141+
142+
Returns:
143+
int: Returns the count of number of nodes.
144+
"""
145+
if self.is_empty():
146+
return 0
147+
148+
count = 0
149+
curr = self.head
150+
while curr is not None:
151+
count += 1
152+
curr = curr.next
153+
154+
return count

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy