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
122 changes: 106 additions & 16 deletions linked_list/linked_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,67 +9,149 @@ def __init__(self, value, next_node = None):
# 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
self.tail = None # keep track of the tail

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I notice you're not using this.


# returns the value in the first node
# returns None if the list is empty
# Time Complexity: ?
# Space Complexity: ?
def get_first(self):
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: ?
def add_first(self, value):
pass
new_node = Node(value, next_node=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) because we arent creating any new data structures, nothings being changed/added
def search(self, value):
pass
current = self.head
if current == None:
return False
while current:
if current.value == value:
return True
current = current.next
notTrue = True
if notTrue == True:
return False
Comment on lines +40 to +48

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Can be simplified

Suggested change
if current == None:
return False
while current:
if current.value == value:
return True
current = current.next
notTrue = True
if notTrue == True:
return False
if current == None:
return False
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):
pass
if self.head == None:
return 0

current = self.head
node_count = 1

while current.next != None:
current = current.next
node_count += 1
if node_count:
return node_count
Comment on lines +65 to +66

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
if node_count:
return node_count
return node_count

# return current.value

# 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):
pass
current = self.head
current_index = 0

while current != None:
if current_index == index:
return current.value
current_index += 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: ?
def get_last(self):
pass
if self.head == None:
return None
current = self.head
while current.next != None:
current = current.next
return current.value

# def get_last(self): # O(1)
# if self.head == None:
# return None

# return self.tail.data

# method that inserts a given value as a new last node in the linked list
# Time Complexity: ?
# Space Complexity: ?
def add_last(self, value):
pass
if self.head == None:
self.head = Node(value, self.head)
else:
current = self.head

while current.next != None:
current = current.next
current.next = Node(value)

# method to return the max value in the linked list
# returns the data value and not the node
def find_max(self):
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

def find(self, value):
current = self.head

while current != None:
if current.data == value:
return True
current = current.next
return False

# method to delete the first node found with specified value
# Time Complexity: ?
# Space Complexity: ?
def delete(self, value):
pass
current = self.head
if current == None:
return None
elif current.value == value:
self.head = current.next

while current.next != None:
if current.next.value != value:
current = current.next
else:
current.next = current.next.next

# method to print all the values in the linked list
# Time Complexity: ?
Expand All @@ -89,8 +171,16 @@ def visit(self):
# Time Complexity: ?
# Space Complexity: ?
def reverse(self):
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
# Time Complexity: ?
Expand All @@ -110,7 +200,7 @@ def find_nth_from_end(self, n):
# returns true if a cycle is found, false otherwise.
# Time Complexity: ?
# Space Complexity: ?
def has_cycle(self):
def find_nth_from_end(self, n):
pass

# Helper method for tests
Expand Down