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
166 changes: 138 additions & 28 deletions linked_list/linked_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,70 +10,173 @@ def __init__(self, value, next_node = None):
class LinkedList:
def __init__(self):
self.head = None # keep the head private. Not accessible outside this class
self.size = 0
self.tail = None

# 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(n)
def get_first(self):
Comment on lines +18 to 20

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

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:
self.head = Node(value)
self.tail = self.head
self.size += 1
else:
new_node = Node(value)
new_node.next = current
current.previous = new_node
self.head = new_node
self.size += 1
Comment on lines +31 to +41

Choose a reason for hiding this comment

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

Could be more compact

Suggested change
current = self.head
if self.head == None:
self.head = Node(value)
self.tail = self.head
self.size += 1
else:
new_node = Node(value)
new_node.next = current
current.previous = new_node
self.head = new_node
self.size += 1
self.head = Node(value, self.head)
self.size += 1
if self.tail is None:
self.tail = 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):
Comment on lines +45 to 47

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:
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(1)
# Space Complexity: O(1)
def length(self):
Comment on lines +56 to 58

Choose a reason for hiding this comment

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

👍

pass
return self.size


# 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: ?
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.

👍 space/time complexity

pass
length = self.length()
current = self.head
counter = 0
if self.head == None and length < index:

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 and length < index:
if self.head == None or length < index:

return None

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

# 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)
# Space Complexity: O(n)
def get_last(self):
Comment on lines +82 to 84

Choose a reason for hiding this comment

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

👍 space/time complexity

pass
#if self.head == None:
# return None
#current = self.head
#while current.next != None:
# current = current.next
#return current.value
if self.head == None:
return None

return self.tail.value

# this may be improved using a tail node

# 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 +99 to 101

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)
#curr = self.head

#if curr is None:
# self.head = new_node
#else:
# while curr.next is not None:
# curr = curr.next
# curr.next = new_node

new_node = Node(value)

if self.head == None:
self.head = new_node
self.tail = self.head
self.size += 1
Comment on lines +114 to +117

Choose a reason for hiding this comment

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

In this scenario, you could also call self.add_first

else:
current = self.head
while current.next is not None:
current = current.next
# current.previous = current.next
current.next = new_node
self.tail = new_node
self.size += 1
# testing?
# 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
max = 0
if self.head == None:
return None
while current != None:
if max < current.value:
max = current.value
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 +143 to 145

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
if current.value == value:
self.head = self.head.next
self.size -= 1
self.head.previous = None
return

while current:
if current.next == None:
if current.value == value:
previous = current.previous
self.tail = previous
previous.next = None
current.previous = None
current = None
self.size -= 1
return
if current.value == value:
next = current.next
previous = current.previous
next.previous = previous
current.previous = None
current.next = None
self.size -= 1
return

current = current.next


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

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
Expand All @@ -86,10 +189,17 @@ 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 +192 to 194

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 != 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