From 8bdd2a24df6f75908b1a71fabd91445916b7d969 Mon Sep 17 00:00:00 2001 From: Jamal Laqdiem Date: Tue, 7 Jul 2026 12:19:50 +0100 Subject: [PATCH 1/3] Create LInkdList optimizing performance --- Sprint-2/implement_linked_list/linked_list.py | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/Sprint-2/implement_linked_list/linked_list.py b/Sprint-2/implement_linked_list/linked_list.py index e69de29..1288122 100644 --- a/Sprint-2/implement_linked_list/linked_list.py +++ b/Sprint-2/implement_linked_list/linked_list.py @@ -0,0 +1,61 @@ +from dataclasses import dataclass +from typing import Any, Optional +# to reduce memory usage we use slots true to tells Python not to create a dynamic __dict__ for every node, +@dataclass(slots=True) +class Node: + value: Any + next: Optional['Node'] = None + previous: Optional['Node'] = None + + +class LinkedList: + def __init__(self) -> None: + self.head: Optional[Node] = None + self.tail: Optional[Node] = None + + def push_head(self, value: Any) -> Node: + new_node = Node(value) + + if not self.head: + self.head = new_node + self.tail = new_node + else: + new_node.next = self.head + self.head.previous = new_node + self.head = new_node + + return new_node + + def pop_tail(self) -> Optional[Any]: + if not self.tail: + return None + + value_to_return = self.tail.value + self.remove(self.tail) + return value_to_return + + def remove(self, node_handle: Optional[Node]) -> None: + if not node_handle: + return + + if node_handle == self.head: + self.head = node_handle.next + if self.head: + self.head.previous = None + else: + self.tail = None + + elif node_handle == self.tail: + self.tail = node_handle.previous + if self.tail: + self.tail.next = None + else: + self.head = None + + else: + if node_handle.previous and node_handle.next: + node_handle.previous.next = node_handle.next + node_handle.next.previous = node_handle.previous + + node_handle.next = None + node_handle.previous = None From ddafbbc137229ea4a1853730c8f06f06227c83de Mon Sep 17 00:00:00 2001 From: Jamal Laqdiem Date: Thu, 9 Jul 2026 14:53:54 +0100 Subject: [PATCH 2/3] Clean the script from redundant checks. --- Sprint-2/implement_linked_list/linked_list.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement_linked_list/linked_list.py b/Sprint-2/implement_linked_list/linked_list.py index 1288122..b413acc 100644 --- a/Sprint-2/implement_linked_list/linked_list.py +++ b/Sprint-2/implement_linked_list/linked_list.py @@ -53,7 +53,8 @@ def remove(self, node_handle: Optional[Node]) -> None: self.head = None else: - if node_handle.previous and node_handle.next: + assert node_handle.previous is not None + assert node_handle.next is not None node_handle.previous.next = node_handle.next node_handle.next.previous = node_handle.previous From 0900984916b013a2f53466048c3a3425ab506e59 Mon Sep 17 00:00:00 2001 From: Jamal Laqdiem Date: Mon, 13 Jul 2026 12:04:22 +0100 Subject: [PATCH 3/3] Changed parameter to 'node: Node' --- Sprint-2/implement_linked_list/linked_list.py | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/Sprint-2/implement_linked_list/linked_list.py b/Sprint-2/implement_linked_list/linked_list.py index b413acc..3732110 100644 --- a/Sprint-2/implement_linked_list/linked_list.py +++ b/Sprint-2/implement_linked_list/linked_list.py @@ -34,29 +34,29 @@ def pop_tail(self) -> Optional[Any]: self.remove(self.tail) return value_to_return - def remove(self, node_handle: Optional[Node]) -> None: - if not node_handle: + def remove(self, node: Node) -> None: + if not node: return - if node_handle == self.head: - self.head = node_handle.next + if node == self.head: + self.head = node.next if self.head: self.head.previous = None else: self.tail = None - elif node_handle == self.tail: - self.tail = node_handle.previous + elif node == self.tail: + self.tail = node.previous if self.tail: self.tail.next = None else: self.head = None else: - assert node_handle.previous is not None - assert node_handle.next is not None - node_handle.previous.next = node_handle.next - node_handle.next.previous = node_handle.previous + assert node.previous is not None + assert node.next is not None + node.previous.next = node.next + node.next.previous = node.previous - node_handle.next = None - node_handle.previous = None + node.next = None + node.previous = None