-
Notifications
You must be signed in to change notification settings - Fork 68
Gloria #52
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: master
Are you sure you want to change the base?
Gloria #52
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 | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -5,71 +5,145 @@ class Node: | |||||||||||
def __init__(self, value, next_node = None): | ||||||||||||
self.value = value | ||||||||||||
self.next = next_node | ||||||||||||
|
||||||||||||
|
||||||||||||
# Defines the singly linked list | ||||||||||||
class LinkedList: | ||||||||||||
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 class | ||||||||||||
self.len = 0 | ||||||||||||
self.tail = None | ||||||||||||
self.max = 0 | ||||||||||||
|
||||||||||||
|
||||||||||||
|
||||||||||||
|
||||||||||||
|
||||||||||||
# returns the value in the first node | ||||||||||||
# returns None if the list is empty | ||||||||||||
# Time Complexity: ? | ||||||||||||
# Space Complexity: ? | ||||||||||||
def get_first(self): | ||||||||||||
Comment on lines
24
to
26
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/space complexity? |
||||||||||||
pass | ||||||||||||
if self.head == None: | ||||||||||||
return None | ||||||||||||
return self.head.value | ||||||||||||
|
||||||||||||
|
||||||||||||
|
||||||||||||
# 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: ? | ||||||||||||
def add_first(self, value): | ||||||||||||
Comment on lines
35
to
37
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/space complexity? |
||||||||||||
pass | ||||||||||||
# next_node = self.head | ||||||||||||
self.head = Node(value, next_node = self.head) | ||||||||||||
self.len += 1 | ||||||||||||
self.update_max(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. Nice little helper |
||||||||||||
|
||||||||||||
|
||||||||||||
# method to find if the linked list contains a node with specified value | ||||||||||||
# returns true if found, false otherwise | ||||||||||||
# Time Complexity: ? | ||||||||||||
# Space Complexity: ? | ||||||||||||
def search(self, value): | ||||||||||||
Comment on lines
46
to
48
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/space complexity? |
||||||||||||
pass | ||||||||||||
node = self.head | ||||||||||||
while node != None: | ||||||||||||
if node.value != value: | ||||||||||||
node = node.next | ||||||||||||
else: | ||||||||||||
return True | ||||||||||||
return False | ||||||||||||
|
||||||||||||
|
||||||||||||
|
||||||||||||
# method that returns the length of the singly linked list | ||||||||||||
# Time Complexity: ? | ||||||||||||
# Space Complexity: ? | ||||||||||||
def length(self): | ||||||||||||
Comment on lines
60
to
62
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/space complexity? |
||||||||||||
pass | ||||||||||||
if self.head == None: | ||||||||||||
return 0 | ||||||||||||
return self.len | ||||||||||||
Comment on lines
+63
to
+65
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. Can be rewritten
Suggested change
|
||||||||||||
|
||||||||||||
|
||||||||||||
# 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: ? | ||||||||||||
def get_at_index(self, index): | ||||||||||||
Comment on lines
71
to
73
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/space complexity? |
||||||||||||
pass | ||||||||||||
node = self.head | ||||||||||||
counter = 0 | ||||||||||||
if self.len < index: | ||||||||||||
return None | ||||||||||||
while node != None: | ||||||||||||
|
||||||||||||
if counter == index: | ||||||||||||
Comment on lines
+79
to
+80
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
|
||||||||||||
return node.value | ||||||||||||
node = node.next | ||||||||||||
counter +=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: ? | ||||||||||||
def get_last(self): | ||||||||||||
Comment on lines
88
to
90
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/space complexity? |
||||||||||||
pass | ||||||||||||
print(self.len) | ||||||||||||
return self.get_at_index(self.len-1) | ||||||||||||
|
||||||||||||
|
||||||||||||
|
||||||||||||
|
||||||||||||
|
||||||||||||
# method that inserts a given value as a new last node in the linked list | ||||||||||||
# Time Complexity: ? | ||||||||||||
# Space Complexity: ? | ||||||||||||
def add_last(self, value): | ||||||||||||
pass | ||||||||||||
|
||||||||||||
if self.len == 0: | ||||||||||||
self.tail = Node(value, next_node = None) | ||||||||||||
self.head = self.tail | ||||||||||||
self.update_max(value) | ||||||||||||
|
||||||||||||
Comment on lines
+103
to
+107
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. You only really use |
||||||||||||
else: | ||||||||||||
self.tail.next = Node(value, next_node = None) | ||||||||||||
self.tail = self.tail.next | ||||||||||||
self.update_max(value) | ||||||||||||
|
||||||||||||
self.len += 1 | ||||||||||||
|
||||||||||||
|
||||||||||||
# method to return the max value in the linked list | ||||||||||||
# returns the data value and not the node | ||||||||||||
def find_max(self): | ||||||||||||
pass | ||||||||||||
return self.max | ||||||||||||
|
||||||||||||
def update_max(self,value): | ||||||||||||
if value >= self.max: | ||||||||||||
self.max = value | ||||||||||||
|
||||||||||||
|
||||||||||||
|
||||||||||||
# method to delete the first node found with specified value | ||||||||||||
# Time Complexity: ? | ||||||||||||
# Space Complexity: ? | ||||||||||||
def delete(self, value): | ||||||||||||
Comment on lines
128
to
130
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/space complexity? |
||||||||||||
pass | ||||||||||||
node = self.head | ||||||||||||
prev_node = None | ||||||||||||
while node != None: | ||||||||||||
if node.value != value: | ||||||||||||
prev_node = node | ||||||||||||
node = node.next | ||||||||||||
else: | ||||||||||||
if prev_node == None: | ||||||||||||
self.head = node.next | ||||||||||||
self.len -= 1 | ||||||||||||
else: | ||||||||||||
prev_node.next = node.next | ||||||||||||
self.len -= 1 | ||||||||||||
break | ||||||||||||
|
||||||||||||
|
||||||||||||
|
||||||||||||
# method to print all the values in the linked list | ||||||||||||
# Time Complexity: ? | ||||||||||||
|
@@ -89,7 +163,24 @@ def visit(self): | |||||||||||
# Time Complexity: ? | ||||||||||||
# Space Complexity: ? | ||||||||||||
def reverse(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. Just noting this is empty. |
||||||||||||
pass | ||||||||||||
# node = self.head | ||||||||||||
# self.head = self.tail | ||||||||||||
# self.tail = node | ||||||||||||
|
||||||||||||
# while node != None: | ||||||||||||
# print("<<<", node.value) | ||||||||||||
# if node.next == None: | ||||||||||||
# break | ||||||||||||
# else: | ||||||||||||
# node_next = node.next | ||||||||||||
# node1 = node.next | ||||||||||||
# node1.next = node | ||||||||||||
# node = node_next | ||||||||||||
|
||||||||||||
|
||||||||||||
|
||||||||||||
|
||||||||||||
|
||||||||||||
|
||||||||||||
## Advanced/ Exercises | ||||||||||||
# returns the value at the middle element in the singly linked list | ||||||||||||
|
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.
You're not actually using the
tail
etc