From 31ec5549ac941beb20952c5850db0d07d066d511 Mon Sep 17 00:00:00 2001 From: Mikiyas-STP Date: Wed, 15 Oct 2025 15:46:16 +0100 Subject: [PATCH 1/3] linked list implementation --- Sprint-2/implement_linked_list/linked_list.py | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/Sprint-2/implement_linked_list/linked_list.py b/Sprint-2/implement_linked_list/linked_list.py index e69de29..eb648cc 100644 --- a/Sprint-2/implement_linked_list/linked_list.py +++ b/Sprint-2/implement_linked_list/linked_list.py @@ -0,0 +1,52 @@ +class Node: + def __init__(self, value): + self.value = value + self.next = None + self.previous = None +class LinkedList: + def __init__(self): + self.head = None + self.tail = None + + def push_head(self, value): + """Add a value to the head. Return the node as a handle.""" + node = Node(value) + node.next = self.head + + if self.head: + self.head.previous = node + self.head = node + if not self.tail: + self.tail = node #list=empty,tail also points to new node + return node + + def pop_tail(self): + if not self.tail: + return None + value = self.tail.value + prev_node = self.tail.previous + if prev_node: + prev_node.next = None + self.tail = prev_node + + + if not self.tail: + self.head = None #If we removed the only element + + return value + + def remove(self, node): + if not node: + return + prev_node = node.previous + next_node = node.next + if prev_node: + prev_node.next = next_node + else: + self.head = next_node + if next_node: + next_node.previous = prev_node + else: + self.tail = prev_node + node.next = None # Disconnect node + node.previous = None From 385268bb7d1f0d5371cd04047e306fde828e85b7 Mon Sep 17 00:00:00 2001 From: Mikiyas-STP Date: Wed, 15 Oct 2025 16:58:54 +0100 Subject: [PATCH 2/3] linked list implememntation --- Sprint-2/implement_linked_list/linked_list.py | 20 ++++++------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/Sprint-2/implement_linked_list/linked_list.py b/Sprint-2/implement_linked_list/linked_list.py index eb648cc..0136f5c 100644 --- a/Sprint-2/implement_linked_list/linked_list.py +++ b/Sprint-2/implement_linked_list/linked_list.py @@ -7,20 +7,16 @@ class LinkedList: def __init__(self): self.head = None self.tail = None - - def push_head(self, value): - """Add a value to the head. Return the node as a handle.""" + def push_head(self, value): #this is to add a value/node to the head/start. node = Node(value) node.next = self.head - if self.head: self.head.previous = node self.head = node if not self.tail: - self.tail = node #list=empty,tail also points to new node + self.tail = node #list=empty,tail also points to new node return node - - def pop_tail(self): + def pop_tail(self): #Remove the value from the tail of the list if not self.tail: return None value = self.tail.value @@ -28,14 +24,10 @@ def pop_tail(self): if prev_node: prev_node.next = None self.tail = prev_node - - if not self.tail: - self.head = None #If we removed the only element - + self.head = None #If only one element is removed. return value - - def remove(self, node): + def remove(self, node): #remove any node if not node: return prev_node = node.previous @@ -48,5 +40,5 @@ def remove(self, node): next_node.previous = prev_node else: self.tail = prev_node - node.next = None # Disconnect node + node.next = None #Disconnecting the node from the rest node.previous = None From 09df9bbcc1d971ca922ab7c24e1d2c5d2ab9bfea Mon Sep 17 00:00:00 2001 From: Mikiyas-STP Date: Wed, 15 Oct 2025 17:09:47 +0100 Subject: [PATCH 3/3] complexity analysis (for my self) --- Sprint-2/implement_linked_list/linked_list.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Sprint-2/implement_linked_list/linked_list.py b/Sprint-2/implement_linked_list/linked_list.py index 0136f5c..d0925f3 100644 --- a/Sprint-2/implement_linked_list/linked_list.py +++ b/Sprint-2/implement_linked_list/linked_list.py @@ -42,3 +42,6 @@ def remove(self, node): #remove any node self.tail = prev_node node.next = None #Disconnecting the node from the rest node.previous = None +# My analysis +# the algorithm used on this linked list is efficient each methods have only O(1) space and time complexity since they operate on a single node at a time +# and at the start of the program we just created a class at a time with similar time and space complexity of O(n) \ No newline at end of file