Skip to content

London | March-2025 | Elhadj Abdoul Diallo | implement_linked_list #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions Sprint-2/implement_linked_list/linked_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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):
node = Node(value)
node.next = self.head
if self.head:
self.head.previous = node
self.head = node
if self.tail is None:
self.tail = node
return node

def pop_tail(self):
if self.tail is None:
return None
node = self.tail
value = node.value
if node.previous:
self.tail = node.previous
self.tail.next = None
else:
self.head = None
self.tail = None
return value

def remove(self, node):
if node.previous:
node.previous.next = node.next
else:
self.head = node.next
if node.next:
node.next.previous = node.previous
else:
self.tail = node.previous
node.next = None
node.previous = None