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
173 changes: 125 additions & 48 deletions linked_list/linked_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,105 +13,181 @@ def __init__(self):

# 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):
Comment on lines +16 to 18

Choose a reason for hiding this comment

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

👍

pass

if self.head:
return self.head.value
else:
return None

# 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):
Comment on lines +26 to 28

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
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):
Comment on lines +35 to 37

Choose a reason for hiding this comment

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

👍

pass
if not self.head:
return False
else:
curr_node = self.head
while curr_node != None:
if curr_node.value == value:
return True
curr_node = curr_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):
Comment on lines +49 to 51

Choose a reason for hiding this comment

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

👍

pass
curr_node = self.head
len = 0
while curr_node != None:
len += 1
curr_node = curr_node.next
return 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: ?
# Time Complexity: O(n)
# Space Complexity: O(1)
def get_at_index(self, index):
Comment on lines +62 to 64

Choose a reason for hiding this comment

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

👍

pass
if index >= self.length():
print("The index is out of range")
return None
curr_index = 0
curr_node = self.head
while index > curr_index:
curr_node = curr_node.next
curr_index += 1
if curr_index == index:
return curr_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: O(n)
# Space Complexity: O(1)
def get_last(self):
Comment on lines +78 to 80

Choose a reason for hiding this comment

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

👍

pass
if not self.head:
return None
curr_node = self.head
while curr_node.next != None:
curr_node = curr_node.next
return curr_node.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):
Comment on lines +89 to 91

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)
if not self.head:
self.head = new_node
return
curr_node = self.head
while curr_node.next != None:
curr_node = curr_node.next
curr_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 not self.head:
return None
curr_node = self.head
max_value = self.head.value
while curr_node != None:
if curr_node.value > max_value:
max_value = curr_node.value
curr_node = curr_node.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(1)
def delete(self, value):
Comment on lines +115 to 117

Choose a reason for hiding this comment

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

👍

pass
if not self.head:
return None
curr_node = self.head
if value == curr_node.value:
self.head = curr_node.next
return
while curr_node != None:
prev_node = curr_node
curr_node = curr_node.next
if value == curr_node.value:
prev_node.next = curr_node.next
return

# method to print all the values in the linked list
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n)
# Space Complexity: O(n)
def visit(self):
Comment on lines +132 to 134

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
curr_node = self.head

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

print(", ".join(helper_list))
return helper_list

# 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(n)
def reverse(self):
Comment on lines +147 to 149

Choose a reason for hiding this comment

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

👍 However the space complexity is O(1)

pass
prev_node = None
curr_node = self.head
while curr_node != None:
next_node = curr_node.next
curr_node.next = prev_node
prev_node = curr_node
curr_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: ?
# Time Complexity: O(n)
# Space Complexity: O(1)
def find_middle_value(self):
Comment on lines +161 to 163

Choose a reason for hiding this comment

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

👍 Good reuse of existing methods

pass
len_list = self.length()
index = len_list // 2
return self.get_at_index(index)

# find the nth node from the end and return its value
# assume indexing starts at 0 while counting to n
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n)
# Space Complexity: O(1)
def find_nth_from_end(self, n):
Comment on lines +170 to 172

Choose a reason for hiding this comment

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

👍

pass
len_list = self.length()
index = len_list - 1 - n
return self.get_at_index(index)

# checks if the linked list has a cycle. A cycle exists if any node in the
# linked list links to a node already visited.
# returns true if a cycle is found, false otherwise.
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n)
# Space Complexity: O(1)
def has_cycle(self):
Comment on lines +180 to 182

Choose a reason for hiding this comment

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

👍 , Nice!

pass
slow_p = self.head
fast_p = self.head
while fast_p and fast_p.next != None:
slow_p = slow_p.next
fast_p = fast_p.next.next
if slow_p == fast_p:
return True
return False

# Helper method for tests
# Creates a cycle in the linked list for testing purposes
Expand All @@ -121,8 +197,9 @@ def create_cycle(self):
return

# navigate to last node
current = self.head
while current.next != None:
current = current.next
curr_node = self.head
while curr_node.next != None:
curr_node = curr_node.next

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

current.next = self.head # make the last node link to first node
8 changes: 1 addition & 7 deletions tests/linked_list_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,11 @@ def test_reverse_will_reverse_five_element_list(list):
for i in range(0, 5):
list.add_first(i)

list.reverse()
list.reverse()

for i in range(0, 5):
assert list.get_at_index(i) == i

@pytest.mark.skip(reason="Going Further methods")
def test_find_middle_value_returns_middle_element_of_five_element_list(list):
list.add_first(10)
list.add_first(30)
Expand All @@ -206,7 +205,6 @@ def test_find_middle_value_returns_middle_element_of_five_element_list(list):
list.add_first(20)
assert list.find_middle_value() == 50

@pytest.mark.skip(reason="Going Further methods")
def test_find_middle_value_returns_element_at_index_two_of_six_element_list(list):
list.add_first(10)
list.add_first(30)
Expand All @@ -216,11 +214,9 @@ def test_find_middle_value_returns_element_at_index_two_of_six_element_list(list
list.add_first(100)
assert list.find_middle_value() == 60

@pytest.mark.skip(reason="Going Further methods")
def test_nth_from_n_when_list_is_empty(list):
assert list.find_nth_from_end(3) == None

@pytest.mark.skip(reason="Going Further methods")
def test_find_nth_from_n_when_length_less_than_n(list):
list.add_first(5)
list.add_first(4)
Expand All @@ -230,7 +226,6 @@ def test_find_nth_from_n_when_length_less_than_n(list):

assert list.find_nth_from_end(6) == None

@pytest.mark.skip(reason="Going Further methods")
def test_find_nth_from_n(list):
list.add_first(1)
list.add_first(2)
Expand All @@ -243,7 +238,6 @@ def test_find_nth_from_n(list):
assert list.find_nth_from_end(3) == 4
assert list.find_nth_from_end(4) == None

@pytest.mark.skip(reason="Going Further methods")
def test_has_cycle(list):
assert list.has_cycle() == False

Expand Down