Skip to content
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
184 changes: 142 additions & 42 deletions linked_list/linked_list.py
Original file line number Diff line number Diff line change
@@ -1,97 +1,197 @@

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

def __init__(self, value, next_node = None):
def __init__(self, value, next_node=None):
self.value = value
self.next = next_node


# composition relationship to class 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: constant, no traversing needed
# Space Complexity: constant, no new data structures
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: constant
# Space Complexity: constant
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)
new_node.next = self.head # assign current head of linked list to the
self.head = new_node # assign new node to 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: linear
# Space Complexity: constant
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
if self.head is None:
return False
current_node = self.head
target_node = Node(value)

Choose a reason for hiding this comment

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

You can create a new node for the target, but it's not necessary. See my suggestion on line 45 ⬇️


# traverse list til reach target node or end of list
while current_node:
if current_node.value == target_node.value:

Choose a reason for hiding this comment

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

current_node.value is an integer, so instead of creating a new node target_node that has value value, you can just compare the current node's value directly to the passed in value.

Suggested change
if current_node.value == target_node.value:
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: ?
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.

⏱🪐 Time and space complexity?

def length(self):
pass
if self.head is None:

Choose a reason for hiding this comment

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

return 0
current_node = self.head
length_of_list = 0

# traverse list til reach end, increment length while traversing.
while current_node:
length_of_list += 1
current_node = current_node.next

return length_of_list

# 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: linear
# Space Complexity: constant

Choose a reason for hiding this comment

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

def get_at_index(self, index):
pass
if index < 0 or self.length() < index:
return None
current_index = 0
current_node = self.head

# traverse list till reach index or end of list
while current_node:
# can add a condition to check the current node is less than index if above does not work
if current_index == index:
break

Choose a reason for hiding this comment

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

How might you refactor your code to eliminate this break statement?

current_node = current_node.next
current_index += 1

return current_node.value

# 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: linear
# Space Complexity: constant
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 is None:
return None
current_node = self.head
tail_node = current_node
prev_node = None

# traverse list till reach end.
while current_node:
prev_node = current_node
current_node = current_node.next
tail_node = prev_node
return tail_node.value

# method that inserts a given value as a new last node in the linked list
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: linear
# Space Complexity: constant
Comment on lines +106 to +107

Choose a reason for hiding this comment

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

💫 Correct on the time and space complexity, however see my comments below ⬇️

def add_last(self, value):
pass
if self.head is None:
self.head = value

Choose a reason for hiding this comment

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

🤔 self.head should be equal to a node object, not an integer like value

return

prev_node = None
current_node = self.head
new_tail_node = Node(value)

# traverse list till reach end.
while current_node:
prev_node = current_node
current_node = current_node.next
# re-assign nodes so last node we found becomes second to last pointing to new tail and new tail points to none
new_tail_node = prev_node
current_node.next = new_tail_node
new_tail_node.next = None
Comment on lines +122 to +124

Choose a reason for hiding this comment

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

Here, after creating the new_tail_node on line 115, you are overwriting it with prev_node which, once the while loop is finished executing, should be the current tail node.

Once the while loop is finished executing current_node is going to be the node after the the current tail, aka None.

Instead, try pointing the current tail (prev_node) to new_tail_node


# method to return the max value in the linked list
# returns the data value and not the node
def find_max(self):
pass
if self.head is None:
return None
current_node = self.head
max_node = current_node

while current_node:
if current_node.value > max_node.value:
max_node = current_node
current_node = current_node.next

Choose a reason for hiding this comment

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

🤔You're creating an infinite loop here since you only reassign current_node to the next node in the list if current_node.value > max_node.value


return max_node.value

# method to delete the first node found with specified value
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: linear
# Space Complexity: constant
def delete(self, value):
pass
if self.head is None:
return
current_node = self.head
next_node = current_node.next
target_value = value
if current_node.value == target_value:
current_node = current_node.next

Choose a reason for hiding this comment

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

This makes current_node equal to what was the second node in the list, but it doesn't redirect self.head to point at the second node in the list

Suggested change
current_node = current_node.next
self.head = current_node.next

return
while current_node:
if next_node.value == target_value:
current_node.next = next_node.next
return
current_node = current_node.next
next_node = next_node.next
return

# method to print all the values in the linked list
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: linear
# Space Complexity: linear
def visit(self):

Choose a reason for hiding this comment

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

helper_list = []
current = self.head
node_values = []
current_node = self.head

while current:
helper_list.append(str(current.value))
current = current.next

print(", ".join(helper_list))
# traverse list,append values along the way.
while current_node:
node_values.append(str(current_node.value))
current_node = current_node.next
print(",".join(node_values))

# 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: linear
# Space Complexity: constant
def reverse(self):

Choose a reason for hiding this comment

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

pass

## Advanced/ Exercises
if self.head is None:
return
current_node = self.head
next_node = None
prev_node = None

# traverse list, reversing pointers along the way
while current_node:
next_node = current_node.next
current_node.next = prev_node
prev_node = current_node
current_node = next_node
self.head = prev_node

## Advanced/ Exercises

# returns the value at the middle element in the singly linked list
# Time Complexity: ?
# Space Complexity: ?
Expand Down Expand Up @@ -125,4 +225,4 @@ def create_cycle(self):
while current.next != None:
current = current.next

current.next = self.head # make the last node link to first node
current.next = self.head # make the last node link to first node