Skip to content

London | Nadika Zavodovska | Module-Complexity | Sprint 2 | Implement linked list #24

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 4 commits 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
57 changes: 57 additions & 0 deletions Sprint-2/implement_linked_list/linked_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# A single node in the list
class ListNode:
def __init__(self, value):
self.value = value
self.previous = None
self.next = None
# The linked list class
class LinkedList:
def __init__(self):
self.head = None
self.tail = None

# Add a value to the front of the list
def push_head(self, value):
new_node = ListNode(value)
new_node.next = self.head

if self.head:
self.head.previous = new_node

self.head = new_node

if self.tail is None:
self.tail = new_node

return new_node

# Remove and return value from the end
def pop_tail(self):
if self.tail is None:
return None

value = self.tail.value

if self.tail.previous:
self.tail = self.tail.previous
self.tail.next = None
else:
self.head = None
self.tail = None

return value

# Remove a node from the list
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.previous = None
node.next = None