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

all tests passing #66

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
95 changes: 83 additions & 12 deletions linked_list/linked_list.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@

# Defines a node in the singly linked list
from unittest.mock import NonCallableMagicMock


class Node:

def __init__(self, value, next_node = None):
Expand All @@ -9,67 +12,127 @@ def __init__(self, value, next_node = None):
# 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: ?
Comment on lines 20 to 21

Choose a reason for hiding this comment

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

What do you think the space and time complexity of this is? Is the time complexity dependent on the length of the linked list? Is there any new variables being created that grow larger depending upon the input?

def get_first(self):
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: ?
Comment on lines 30 to 31

Choose a reason for hiding this comment

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

What do you think the space and time complexity of this is? Is the time complexity dependent on the length of the linked list? Is there any new variables being created that grow larger depending upon the input?

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
new_node = Node(value, self.head)
if self.head == None:
self.tail = 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: ?
Comment on lines 40 to 41

Choose a reason for hiding this comment

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

What do you think the space and time complexity of this is? Is the time complexity dependent on the length of the linked list? Is there any new variables being created that grow larger depending upon the input?

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 = self.head
while current != None:

Choose a reason for hiding this comment

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

nice! one way we can shorten this is to:

Suggested change
while current != None:
while current:

if current.value == value:
return True
current = current.next
return False

# method that returns the length of the singly linked list
# Time Complexity: ?
# Space Complexity: ?
Comment on lines 51 to 52

Choose a reason for hiding this comment

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

What do you think the space and time complexity of this is? Is the time complexity dependent on the length of the linked list? Is there any new variables being created that grow larger depending upon the input?

def length(self):
pass
temp = self.head
count = 0
while temp:
count += 1
temp = temp.next
Comment on lines +54 to +58

Choose a reason for hiding this comment

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

this works fine! just curious why you named it temp instead of keeping with current naming convention

return count

# 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: ?
Comment on lines 64 to 65

Choose a reason for hiding this comment

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

What do you think the space and time complexity of this is? Is the time complexity dependent on the length of the linked list? Is there any new variables being created that grow larger depending upon the input?

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
current = self.head
count = 0
while current:
if count == index:
return current.value
count += 1
current = current.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: ?
Comment on lines 78 to 79

Choose a reason for hiding this comment

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

What do you think the space and time complexity of this is? Is the time complexity dependent on the length of the linked list? Is there any new variables being created that grow larger depending upon the input?

def get_last(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
return self.tail.value

# method that inserts a given value as a new last node in the linked list
# Time Complexity: ?
# Space Complexity: ?
Comment on lines 86 to 87

Choose a reason for hiding this comment

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

What do you think the space and time complexity of this is? Is the time complexity dependent on the length of the linked list? Is there any new variables being created that grow larger depending upon the input?

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.

👍

pass
current = self.head
new_node = Node(value)
if self.head == None:
self.tail = new_node
self.head = new_node
return
while current.next != None:
current = current.next
current.next = new_node
self.tail = current.next
Comment on lines +95 to +98

Choose a reason for hiding this comment

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

do we need this while loop? we already have a pointer to the last node because of self.tail, so we can just do this:

Suggested change
while current.next != None:
current = current.next
current.next = new_node
self.tail = current.next
self.tail.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
current = self.head
if self.head == None:
return None
max = 0
while current != None:
if current.value > max:
max = current.value
current = current.next
return max

# method to delete the first node found with specified value
# Time Complexity: ?
# Space Complexity: ?
Comment on lines 114 to 115

Choose a reason for hiding this comment

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

What do you think the space and time complexity of this is? Is the time complexity dependent on the length of the linked list? Is there any new variables being created that grow larger depending upon the input?

def delete(self, value):
pass

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

current = self.head
while current.next != None:
if current.value == value:
current.next = current.next.next
return
prev = current
current = current.next
if current.value == value:
prev.next = None
self.tail = prev
Comment on lines +126 to +133

Choose a reason for hiding this comment

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

We want to stop a node earlier in order to drop the deleted node in one line:

Suggested change
if current.value == value:
current.next = current.next.next
return
prev = current
current = current.next
if current.value == value:
prev.next = None
self.tail = prev
if current.next.value == value:
current.next = current.next.next
current = current.next




# method to print all the values in the linked list
# Time Complexity: ?
Expand All @@ -89,7 +152,15 @@ def visit(self):
# Time Complexity: ?
# Space Complexity: ?
Comment on lines 152 to 153

Choose a reason for hiding this comment

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

What do you think the space and time complexity of this is? Is the time complexity dependent on the length of the linked list? Is there any new variables being created that grow larger depending upon the input?

def reverse(self):

Choose a reason for hiding this comment

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

👍

pass
previous = None
current = self.head

while current != None:
next = current.next
current.next = previous
previous = current
current = next
self.head = previous

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