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
147 changes: 122 additions & 25 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 cgi import print_environ_usage


class Node:

def __init__(self, value, next_node = None):
Expand All @@ -13,63 +16,144 @@ 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 +19 to 21

Copy link
Copy Markdown

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
else:
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(n)
# Space Complexity: O(1)
def add_first(self, value):
Comment on lines +29 to 31

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍 Time complexity is O(1)

pass
node = Node(value)
node.next = self.head
self.head = node

return node
Comment on lines +32 to +36

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggested change
node = Node(value)
node.next = self.head
self.head = node
return node
self.head = Node(value, self.head)
return 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: ?
def search(self, value):
Comment on lines 40 to 42

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍 Time/space complexity?

pass
current = self.head

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: ?
def length(self):
Comment on lines 52 to 54

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍 Time/space complexity?

pass

current = self.head
counter = 0

while current:
counter +=1
current = current.next

return counter

# 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 +68 to 70

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍

pass

current = self.head
counter = 0

if current == None:
return None
Comment on lines +75 to +76

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Because of the while loop you don't need the if here.

Suggested change
if current == None:
return None


while (current):
if (counter == index):
return current.value
counter += 1
current = current.next

return current


# 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 +89 to 91

Copy link
Copy Markdown

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 current == 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(n)
# Space Complexity: O(n)
def add_last(self, value):
Comment on lines +103 to 105

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍

pass

node = Node(value)
current = self.head

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

while current.next != None:
current = current.next

current.next = node

# method to return the max value in the linked list
# returns the data value and not the node
# Time Complexity: O(n)
# Space Complexity: O(1)
def find_max(self):
Comment on lines +121 to 123

Copy link
Copy Markdown

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 current == None:
return None
else:
max = self.head.value

while (current):
if (max < current.value):
max = current.value
else:
current = current.next

return 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):
Comment on lines +141 to 143

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍

pass

current = self.head
previous = None

while current:
if current.value == value:
if previous:
previous.next = current.next
else:
self.head = current.next

previous = current
current = current.next

# method to print all the values in the linked list
# Time Complexity: ?
Expand All @@ -86,10 +170,23 @@ 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):
Comment on lines +173 to 175

Copy link
Copy Markdown

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

if current == None:
return None

while current:
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