-
Notifications
You must be signed in to change notification settings - Fork 87
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
Nourhan - Spruce - C16 #68
base: master
Are you sure you want to change the base?
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 | ||||||
---|---|---|---|---|---|---|---|---|
@@ -1,75 +1,135 @@ | ||||||||
|
||||||||
# Defines a node in the singly linked list | ||||||||
class Node: | ||||||||
''' | ||||||||
Defines a node in the singly linked list | ||||||||
''' | ||||||||
|
||||||||
def __init__(self, value, next_node = None): | ||||||||
def __init__(self, value, next_node=None): | ||||||||
self.value = value | ||||||||
self.next = next_node | ||||||||
|
||||||||
# Defines the singly linked list | ||||||||
|
||||||||
class LinkedList: | ||||||||
''' | ||||||||
Defines the singly linked list | ||||||||
''' | ||||||||
|
||||||||
def __init__(self): | ||||||||
self.head = None # keep the head private. Not accessible outside this class | ||||||||
self.head = None # keep the head private. Not accessible outside this class | ||||||||
|
||||||||
# returns the value in the first node | ||||||||
# returns None if the list is empty | ||||||||
# Time Complexity: ? | ||||||||
# Space Complexity: ? | ||||||||
# Time Complexity: O(1) | ||||||||
# Space Complexity: O(1) | ||||||||
def get_first(self): | ||||||||
pass | ||||||||
|
||||||||
if self.head is not None: | ||||||||
return self.head.value | ||||||||
else: | ||||||||
return None | ||||||||
|
||||||||
# method to add a new node with the specific data value in the linked list | ||||||||
# insert the new node at the beginning of the linked list | ||||||||
# Time Complexity: ? | ||||||||
# Space Complexity: ? | ||||||||
# Time Complexity: O(1) | ||||||||
# Space Complexity: O(1) | ||||||||
|
||||||||
def add_first(self, value): | ||||||||
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. ✨ |
||||||||
pass | ||||||||
self.head = Node(value, next_node=self.head) | ||||||||
|
||||||||
# method to find if the linked list contains a node with specified value | ||||||||
# returns true if found, false otherwise | ||||||||
# Time Complexity: ? | ||||||||
# Space Complexity: ? | ||||||||
# Time Complexity: O(n) | ||||||||
# Space Complexity: O(1) | ||||||||
def search(self, value): | ||||||||
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. ✨ |
||||||||
pass | ||||||||
current_node = self.head | ||||||||
while current_node is not None: | ||||||||
if current_node.value == value: | ||||||||
return True | ||||||||
current_node = current_node.next | ||||||||
return False | ||||||||
|
||||||||
# method that returns the length of the singly linked list | ||||||||
# Time Complexity: ? | ||||||||
# Space Complexity: ? | ||||||||
# Time Complexity: O(n) | ||||||||
# Space Complexity: O(1) | ||||||||
def length(self): | ||||||||
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. ✨ |
||||||||
pass | ||||||||
current_node = self.head | ||||||||
length = 0 | ||||||||
while current_node is not None: | ||||||||
length += 1 | ||||||||
current_node = current_node.next | ||||||||
return length | ||||||||
|
||||||||
# method that returns the value at a given index in the linked list | ||||||||
# index count starts at 0 | ||||||||
# returns None if there are fewer nodes in the linked list than the index value | ||||||||
# Time Complexity: ? | ||||||||
# Space Complexity: ? | ||||||||
# Time Complexity: O(n) | ||||||||
# Space Complexity: O(1) | ||||||||
def get_at_index(self, index): | ||||||||
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. ✨ |
||||||||
pass | ||||||||
current_node = self.head | ||||||||
current_index = 0 | ||||||||
while current_node is not None: | ||||||||
if index == current_index: | ||||||||
return current_node.value | ||||||||
current_node = current_node.next | ||||||||
current_index += 1 | ||||||||
return None | ||||||||
|
||||||||
# method that returns the value of the last node in the linked list | ||||||||
# returns None if the linked list is empty | ||||||||
# Time Complexity: ? | ||||||||
# Space Complexity: ? | ||||||||
# Time Complexity: O(n) | ||||||||
# Space Complexity: O(1) | ||||||||
def get_last(self): | ||||||||
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. ✨ |
||||||||
pass | ||||||||
current_node = self.head | ||||||||
while current_node is not None: | ||||||||
if current_node.next is None: | ||||||||
return current_node.value | ||||||||
current_node = current_node.next | ||||||||
return None | ||||||||
|
||||||||
# method that inserts a given value as a new last node in the linked list | ||||||||
# Time Complexity: ? | ||||||||
# Space Complexity: ? | ||||||||
# Time Complexity: O(n) | ||||||||
# Space Complexity: O(1) | ||||||||
def add_last(self, value): | ||||||||
pass | ||||||||
last_node = self.head | ||||||||
while last_node is not None: | ||||||||
if last_node.next is None: | ||||||||
break | ||||||||
Comment on lines
+93
to
+94
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. 🤓 We generally like to avoid break statements where possible. How might you refactor your code to eliminate this one? |
||||||||
last_node = last_node.next | ||||||||
|
||||||||
if last_node is not None: | ||||||||
last_node.next = Node(value) | ||||||||
else: | ||||||||
self.head = Node(value) | ||||||||
|
||||||||
# method to return the max value in the linked list | ||||||||
# returns the data value and not the node | ||||||||
def find_max(self): | ||||||||
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. ✨ |
||||||||
pass | ||||||||
current_node = self.head | ||||||||
if current_node is None: | ||||||||
return None | ||||||||
max_val = current_node.value | ||||||||
while current_node is not None: | ||||||||
max_val = max(current_node.value, max_val) | ||||||||
current_node = current_node.next | ||||||||
return max_val | ||||||||
|
||||||||
# method to delete the first node found with specified value | ||||||||
# Time Complexity: ? | ||||||||
# Space Complexity: ? | ||||||||
# Time Complexity: O(n) | ||||||||
# Space Complexity: O(1) | ||||||||
def delete(self, value): | ||||||||
pass | ||||||||
# Head has the value to delete | ||||||||
if self.head is not None\ | ||||||||
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.
Suggested change
|
||||||||
and self.head.value == value: | ||||||||
self.head = self.head.next | ||||||||
return | ||||||||
|
||||||||
current_node = self.head | ||||||||
# value to delete is in not head | ||||||||
while current_node is not None: | ||||||||
next_node = current_node.next | ||||||||
if next_node is not None\ | ||||||||
and next_node.value == value: | ||||||||
current_node.next = next_node.next | ||||||||
return | ||||||||
current_node = next_node | ||||||||
|
||||||||
# method to print all the values in the linked list | ||||||||
# Time Complexity: ? | ||||||||
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. ⏱🪐 Time and space complexity? |
||||||||
|
@@ -81,20 +141,36 @@ def visit(self): | |||||||
while current: | ||||||||
helper_list.append(str(current.value)) | ||||||||
current = current.next | ||||||||
|
||||||||
print(", ".join(helper_list)) | ||||||||
|
||||||||
# method to reverse the singly linked list | ||||||||
# note: the nodes should be moved and not just the values in the nodes | ||||||||
# Time Complexity: ? | ||||||||
# Space Complexity: ? | ||||||||
# Time Complexity: O(n) | ||||||||
# Space Complexity: O(n) | ||||||||
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. ✨ |
||||||||
def reverse(self): | ||||||||
pass | ||||||||
|
||||||||
node_list = [] | ||||||||
current_node = self.head | ||||||||
while current_node is not None: | ||||||||
node_list.append(current_node) | ||||||||
current_node = current_node.next | ||||||||
|
||||||||
for index in range(len(node_list)-1, 0, -1): | ||||||||
try: | ||||||||
node_list[index].next = node_list[index-1] | ||||||||
except IndexError: | ||||||||
node_list[index].next = None | ||||||||
|
||||||||
try: | ||||||||
self.head = node_list[-1] | ||||||||
except IndexError: | ||||||||
self.head = None | ||||||||
|
||||||||
## Advanced/ Exercises | ||||||||
# returns the value at the middle element in the singly linked list | ||||||||
# Time Complexity: ? | ||||||||
# Space Complexity: ? | ||||||||
|
||||||||
def find_middle_value(self): | ||||||||
pass | ||||||||
|
||||||||
|
@@ -125,4 +201,4 @@ def create_cycle(self): | |||||||
while current.next != None: | ||||||||
current = current.next | ||||||||
|
||||||||
current.next = self.head # make the last node link to first node | ||||||||
current.next = self.head # make the last node link to first 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.
✨