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
60 changes: 60 additions & 0 deletions Sprint-2/implement_linked_list/linked_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
class LinkedList:
class clsNode:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggestion: Look up "Python naming convention for inner class"?

def __init__(self, value):
self.next = None
self.previous = None
self.value = value
Comment on lines +3 to +6

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?


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

def push_head(self, value):
new_node = self.clsNode(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

if self.head == self.tail:
self.head = None
self.tail = None
self.size -= 1
return removed_value
self.tail = self.tail.previous
self.tail.next = None
self.size -= 1
Comment on lines +30 to +37

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

  • The .next and .previous were cleared when a node is removed in removed() but not in this method. (Inconsistency)

  • You could consider delegating the node removing task to remove() -- less code to maintain.

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