Skip to content

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
113 changes: 102 additions & 11 deletions linked_list/linked_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment on lines +13 to +16
Copy link
Author

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






# 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
Copy link
Author

Choose a reason for hiding this comment

The 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
Copy link
Author

Choose a reason for hiding this comment

The 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)
Copy link
Author

Choose a reason for hiding this comment

The 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
Copy link
Author

Choose a reason for hiding this comment

The 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
Copy link
Author

Choose a reason for hiding this comment

The 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
Copy link
Author

Choose a reason for hiding this comment

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

Can be rewritten

Suggested change
if self.head == None:
return 0
return self.len
return self.len



# 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
Copy link
Author

Choose a reason for hiding this comment

The 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
Copy link
Author

Choose a reason for hiding this comment

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

Suggested change
if counter == index:
if counter == index:

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
Copy link
Author

Choose a reason for hiding this comment

The 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
Copy link
Author

Choose a reason for hiding this comment

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

You only really use tail here, but you would potentially need to update it in add_first and remove_first etc.

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
Copy link
Author

Choose a reason for hiding this comment

The 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: ?
Expand All @@ -89,7 +163,24 @@ def visit(self):
# Time Complexity: ?
# Space Complexity: ?
def reverse(self):
Copy link
Author

Choose a reason for hiding this comment

The 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
Expand Down