Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Citlalli-Cedar #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# Linked-List

Using object oriented design constructs, define a Node class and LinkedList class for a singly linked list. Each node has integer data value and a link to the next node. The linked list class has a head node and the following methods defined.

Expand Down
166 changes: 126 additions & 40 deletions linked_list/linked_list.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@

# Defines a node in the singly linked list
from ast import Delete
import re


class Node:

def __init__(self, value, next_node = None):
Expand All @@ -9,94 +13,176 @@ 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


# 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):
pass

if self.head:
return self.head.value
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):
pass

self.head = Node(value, 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):
pass
current_node = self.head
if current_node == None:
return False
while current_node:
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: ?
# Time Complexity: O(n)
# Space Complexity: O(1)
def length(self):
pass
count = 0
current_node = self.head
while current_node != None:
current_node = current_node.next
count +=1
return count

# 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: 0(1)
def get_at_index(self, index):
pass
count =0
current_node = self.head

while current_node != None:
if count == index:
return current_node.value
count += 1
current_node = current_node.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: ?
# Time Complexity: O(n)
# Space Complexity: O(1)
def get_last(self):
pass
current_node = self.head
if current_node == None:
return None

while current_node.next:
current_node = current_node.next

return current_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):
pass
if self.head == None:
self.add_first(value)
else:
new_node = Node(value)
current_node = self.head

while current_node.next:
current_node = current_node.next
current_node.next = new_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):
pass
if self.head == None:
return None

current_node = self.head
max_value = self.head.value

while current_node:
if current_node.value > max_value:
max_value = current_node.value
current_node = current_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):
pass

if not self.head:
return
elif self.head.value == value:
self.head = self.head.next
return True
else:
current = self.head
while current.next and current.next.value != value:
current = current.next
current.next = current.next.next


# 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):
helper_list = []
current = self.head
current_node = self.head

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

print(", ".join(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(1)
def reverse(self):
pass

if not self.head:
return

current_node = self.head
previous_node = None

while current_node:
next = current_node.next
current_node.next = previous_node
previous_node = current_node
current_node = next

self.head = previous_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):
pass
if not self.head:
return None

return self.get_at_index(int(self.length() / 2))


# find the nth node from the end and return its value
# assume indexing starts at 0 while counting to n
Expand Down