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
53 changes: 53 additions & 0 deletions Sprint-2/implement_linked_list/linked_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
class Node:
__slots__ = ("value", "previous", "next")
Copy link

Choose a reason for hiding this comment

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

Good practice.


def __init__(self, value):
self.value = value
self.previous = None
self.next = None


class LinkedList:
def __init__(self):
self.head = None
self.tail = None

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

if self.head is None:
# empty list
self.head = self.tail = node
else:
node.next = self.head
self.head.previous = node
self.head = node

return node #This is the handle

def pop_tail(self):
if self.tail is None:
raise IndexError("Pop from empty list")

node = self.tail
self.remove(node)

return node.value

def remove(self, node):
if node.previous:
node.previous.next = node.next
else:
# removing head
self.head = node.next

if node.next:
node.next.previous = node.previous
else:
# removing the tail
self.tail = node.previous

# Clean up references

node.previous = None
node.next = None
Comment on lines +52 to +53
Copy link

Choose a reason for hiding this comment

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

Why not also clean up references in pop_tail()?

Copy link
Author

Choose a reason for hiding this comment

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

Done thank you.