Skip to content
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
55 changes: 55 additions & 0 deletions Sprint-2/implement_linked_list/linked_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
class Node:
def __init__(self, value):
self.value = value
self.next = None
self.previous = None


class LinkedList:
def __init__(self):
#first
self.head = None
#last
Comment on lines +10 to +12
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would probably make the comment more readable if there is a space after the # character.

self.tail = None

def push_head(self, value):
new_node = Node(value)

if self.head is None:
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 self.tail is None:
return None

removed = self.tail
value = removed.value

self.remove(removed)

return value

def remove(self, node):
if node is None:
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 = None
node.previous = None
Loading