forked from AdaGold/linked-list
-
Notifications
You must be signed in to change notification settings - Fork 66
Al Leonard - Linked List #48
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
Open
Alli-Oops
wants to merge
2
commits into
Ada-C15:master
Choose a base branch
from
Alli-Oops:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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 | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| # 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can be simplified
Suggested change
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| # 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||
| # 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: ? | ||||||||||||||||||||||||||||||||||
|
|
@@ -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: ? | ||||||||||||||||||||||||||||||||||
|
|
@@ -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 | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.