Skip to content
Closed
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
52 changes: 52 additions & 0 deletions Sprint-2/implement_linked_list/linked_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
class Node:
def __init__(self, value):
self.value = value
self.next = None
self.previous = None
class LinkedList:
def __init__(self):
self.head = None
self.tail = None
def push_head(self, value): #this is to add a value/node to the head/start.
node = Node(value)
node.next = self.head
if self.head:
self.head.previous = node
self.head = node
if not self.tail:
self.tail = node #list=empty,tail also points to new node
return node
def pop_tail(self):
if not self.tail:
return None
removed_node = self.tail
value = removed_node.value
prev_node = removed_node.previous
if prev_node:
prev_node.next = None

self.tail = prev_node
if not self.tail:
self.head = None
removed_node.previous = None
removed_node.next = None
Copy link

Choose a reason for hiding this comment

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

This is probably not needed as removed_node.next should always be None.

return value

def remove(self, node): #remove any node
if not node:
return
prev_node = node.previous
next_node = node.next
if prev_node:
prev_node.next = next_node
else:
self.head = next_node
if next_node:
next_node.previous = prev_node
else:
self.tail = prev_node
node.next = None #Disconnecting the node from the rest
node.previous = None
Comment on lines +48 to +49
Copy link

Choose a reason for hiding this comment

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

Why not do something similar to the last node removed 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.

Hi @cjyuan i have implemented it as suggested. actually it is a good practice to be consistent Thank you 4b74bf3

Copy link
Author

Choose a reason for hiding this comment

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

let me know if i could close this PR

# My analysis
# the algorithm used on this linked list is efficient each methods have only O(1) space and time complexity since they operate on a single node at a time
# and at the start of the program we just created a class at a time with similar time and space complexity of O(n)