Skip to content
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

Diana - Pine #74

Open
wants to merge 1 commit 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
161 changes: 126 additions & 35 deletions linked_list/linked_list.py
Original file line number Diff line number Diff line change
@@ -1,79 +1,159 @@

# Defines a node in the singly linked list
class Node:
from types import new_class


def __init__(self, value, next_node = None):
class Node:
def __init__(self, value, next_node = None, prev_node = None):
self.value = value
self.next = next_node
self.previous = prev_node #This line because this is a doubly linked list

Choose a reason for hiding this comment

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

😎 Ooh doubly linked list


# 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 this class
self.tail = None

# 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):

Choose a reason for hiding this comment

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

pass

if self.head is 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: ?
# Time Complexity: O(1)
# Space Complexity: O(1)
def add_first(self, value):

Choose a reason for hiding this comment

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

pass
if self.head is None:
self.head = self.tail = Node(value)

else:
new_node = Node(value, self.head)
self.head.previous = new_node
self.head = new_node

# 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):

Choose a reason for hiding this comment

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

pass
current_node = self.head

if current_node == None:
return False

while current_node:
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):

Choose a reason for hiding this comment

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

pass
len_ll = 0
current_node = self.head

while current_node != None:
len_ll +=1
current_node = current_node.next
return len_ll

# 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):

Choose a reason for hiding this comment

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

pass
count = 0
current_node = self.head

while current_node != None:
if count == index:
return current_node.value
count += 1
current_node = current_node.next
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):

Choose a reason for hiding this comment

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

👀 Since you have a doubly linked list you could just return the tail pointer for an O(1) time solution

pass
head = self.head
if head == None:
return None

while head.next:
head = head.next

return head.value

# 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):

Choose a reason for hiding this comment

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

👀 Again, this works, but take advantage of that tail pointer!

pass
if self.head == None:
self.add_first(value)
else:
new_node = Node(value)
current_node = self.head

while current_node.next:
current_node = current_node.next
current_node.next = new_node


# method to return the max value in the linked list
# returns the data value and not the node
def find_max(self):

Choose a reason for hiding this comment

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

pass
if self.head == None:
return None

head = self.head
max_value = self.get_at_index(0)

while head:
if head.value > max_value:
max_value = head.value
head = head.next

return max_value

# method to delete the first node found with specified value
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n)
# Space Complexity: O(n)
def delete(self, value):

Choose a reason for hiding this comment

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

pass

new = self.head

if new != None:
if new.value == value:
self.head = new.next
new = None
return

while new:
if new.value == value:
break
Comment on lines +143 to +144

Choose a reason for hiding this comment

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

We generally try and avoid using break statements unless really necessary. Might you be able to refactor your code to eliminate this?

prev = new
new = new.next
if new == None:
return

prev.next = new.next
new = None


# method to print all the values in the linked list
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n)
# Space Complexity: O(1)

Choose a reason for hiding this comment

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

🪐 Space complexity will be O(n) here because the function creates a helper_list which will end up holding all the nodes in the linked list

def visit(self):
helper_list = []
current = self.head
Expand All @@ -86,11 +166,22 @@ def visit(self):

# 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(1)
def reverse(self):

Choose a reason for hiding this comment

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

Since you have a doubly linked list you also need to think about how you redirect your previous and tail pointers.

pass

if not self.head:
return
current_node = self.head
previous_node = None

while current_node:
next = current_node.next
current_node.next = previous_node
previous_node = current_node
current_node = next

self.head = previous_node

## Advanced/ Exercises
# returns the value at the middle element in the singly linked list
# Time Complexity: ?
Expand Down