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

def __init__(self, value):
Comment on lines +3 to 5

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

In terms of code formatting, the spacing around the , in the parameter list is not consistent.

Prettier won't work for Python, so you may need to install different formatting tools for Python.

self.next = None
self.previous = None
self.value = value
Comment on lines +5 to +8

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

May I suggest exploring the use of __slots__ to reduce memory usage?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thank you for the suggestion. I've added __slots__ to reduce memory usage.


def __init__(self):
self.head = None
self.tail = None
self.size = 0

def push_head(self, value):
new_node = self.Node(value=value)
if self.head == None:
self.head = new_node
self.tail = new_node
else:
new_node.next = self.head
self.head.previous = new_node
self.head = new_node
self.size += 1
return new_node

def pop_tail(self):
if self.tail == None:
return
removed_value = self.tail.value

self.remove(self.tail)
return removed_value


def remove(self, node):
if node == None:
return
if node == self.head and node == self.tail:
self.head = None
self.tail = None

elif node == self.head:
self.head = self.head.next
self.head.previous = None

elif node == self.tail:
self.tail = self.tail.previous
self.tail.next = None

else:
node.next.previous = node.previous
node.previous.next = node.next
node.next = None
node.previous = None
self.size -= 1
Loading