-
-
Notifications
You must be signed in to change notification settings - Fork 18
London | 25-SDC-July | Mikiyas Gebremichael | Sprint 2 | Implement a linked list in Python #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not do something similar to the last node removed in
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment.
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.nextshould always beNone.