Skip to content
Open
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions linked_list/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Node:
def __init__(self, value):
self.data = value
self.next = None
130 changes: 99 additions & 31 deletions linked_list/linked_list.py
Original file line number Diff line number Diff line change
@@ -1,75 +1,136 @@

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

Choose a reason for hiding this comment

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

where is this coming from? You've created your own Node class on line 6, so you don't need to import a third-party class

#from sys import last_value

class Node:

Choose a reason for hiding this comment

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

👍

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

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.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
self.head = Node(value, 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):

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.

we can also shorten this 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: ?
# 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
current = self.head
count = 0
while current:
count += 1
current = current.next
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: ?
# 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
current = self.head #initialise temp
count = 0 #index of current node
while (current):
if (count == index):
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.

oops! Looks like JavaScript crept in here

Suggested change
while (current):
if (count == index):
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: ?
# Time Complexity: O(1)

Choose a reason for hiding this comment

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

This should be O(n). The while loop is dependent upon the length of the linked list (ie, the input) to find the last node's value

# Space Complexity: O(1)
def get_last(self):
pass
current = self.head
if self.head == None:
return None
while current.next != None:
current = current.next
return current.value

# method that inserts a given value as a new last node in the linked list
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(1)

Choose a reason for hiding this comment

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

This should be O(n). The while loop is dependent upon the length of the linked list (ie, the input) to find the last node, and then add the new node to it

# Space Complexity: O(1)
def add_last(self, value):
pass

new_node = Node(value)
if self.head == None:
self.head = new_node
return
last = self.head
while (last.next):
last = last.next
last.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):
pass
head = self.head
if (self.head == None):

Choose a reason for hiding this comment

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

Suggested change
if (self.head == None):
if self.head == None:

return None
else:
max = head.value

while (head != None):

Choose a reason for hiding this comment

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

Suggested change
while (head != None):
while head:

if head.value > max:
max = head.value
head = head.next
return max
#print("Maximum value node in the list: " + str(max))

Choose a reason for hiding this comment

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

Suggested change
#print("Maximum value node in the list: " + str(max))


# 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
if self.head is None:
return

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

current = self.head
current = self.head
Comment on lines +124 to +125

Choose a reason for hiding this comment

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

you have this twice

Suggested change
current = self.head
current = self.head
current = self.head

while current.next is not None:

Choose a reason for hiding this comment

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

Suggested change
while current.next is not None:
while current.next:

if value == current.next.value:
break
current = current.next
if current.next is None:
print("Node not found")
else:
current.next = current.next.next
Comment on lines +127 to +133

Choose a reason for hiding this comment

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

There's a couple if statements here that aren't really doing anything but stop the loop or print. It makes it hard to read what is important here. I think we can shorten it:

Suggested change
if value == current.next.value:
break
current = current.next
if current.next is None:
print("Node not found")
else:
current.next = current.next.next
if value == current.next.value:
current.next = current.next.next
current = current.next

Since there could be more than one node with the same value, we don't want to break


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

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):
pass
prev = None
current = self.head
while current is not None:

Choose a reason for hiding this comment

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

Suggested change
while current is not None:
while current:

next = current.next
current.next = prev
prev = current
current = next
self.head = prev

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