-
-
Notifications
You must be signed in to change notification settings - Fork 14
London | 25-SDC-July | Andrei Filippov | Sprint 2 | Implement a linked list in Python #28
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
base: main
Are you sure you want to change the base?
London | 25-SDC-July | Andrei Filippov | Sprint 2 | Implement a linked list in Python #28
Conversation
|
|
||
| def push_head(self, element): | ||
| node = Node(element) | ||
| if self.head and self.tail: |
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.
Is it necessary to check also self.tail?
| node.next = self.head | ||
| self.head.previous = node | ||
| self.head = node | ||
| elif not self.head and not self.tail: |
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 line could probably be simplified.
| if self.head != node and self.tail != node: | ||
| node.previous.next = node.next | ||
| node.next.previous = node.previous | ||
| node.previous, node.next = None, None | ||
| elif self.head == node and self.tail != node: | ||
| node.next.previous = None | ||
| self.head = node.next | ||
| node.previous, node.next = None, None | ||
| elif self.head != node and self.tail == node: | ||
| node.previous.next = None | ||
| self.tail = node.previous | ||
| node.previous, node.next = None, None | ||
| elif self.head == node and self.tail == node: |
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.
The conditions can be simplified (reduce the amount of checking needed) as:
if self.head == node:
if self.tail == node:
# Case 1: head == node, tail == node
else:
# Case 2: head == node, tail != node
else;
if self.tail == node:
# Case 3: head != node, tail == node
else:
# Case 4: head != node, tail != nodeOnly two comparisons needed.
Learners, PR Template
Self checklist
Changelist
Implemented a linked list in Python.
Questions
no questions