From ce652fd1db4e5495f10a2a5c1d99a84d36ad6567 Mon Sep 17 00:00:00 2001 From: zohrehKazemianpour <129424353+zohrehKazemianpour@users.noreply.github.com> Date: Sat, 25 Apr 2026 12:30:20 +0100 Subject: [PATCH] Implement doubly linked list --- Sprint-2/implement_linked_list/linked_list.py | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/Sprint-2/implement_linked_list/linked_list.py b/Sprint-2/implement_linked_list/linked_list.py index e69de29..8ab029e 100644 --- a/Sprint-2/implement_linked_list/linked_list.py +++ b/Sprint-2/implement_linked_list/linked_list.py @@ -0,0 +1,54 @@ +class Node: + def __init__(self, value): + self.value = value + self.next = None # Pointer to the next node in the list. + self.previous = None # a pointer to the previous node so the removal can be done in constant time + +class LinkedList: + def __init__(self): + self.head = None + self.tail = None + + def push_head(self, value): + new_node = Node(value) + if not self.head: + self.head = 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): + if not self.tail: + return None + + removed_value = self.tail.value + if self.head == self.tail: + self.head = self.tail = None + else: + self.tail = self.tail.previous + self.tail.next = None + + return removed_value + + def remove(self, node): + if not node: + return + + if node == self.head: + self.head = node.next + + + if node == self.tail: + self.tail = node.previous + + + if node.previous: + node.previous.next = node.next + if node.next: + node.next.previous = node.previous + + + node.next = node.previous = None \ No newline at end of file