-
-
Notifications
You must be signed in to change notification settings - Fork 47.1k
Added new Algorithm to find middle element of Linked List #1822
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
Merged
Merged
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
99dc0fb
Added new Algorithm to find middle element of Linked List
meSajied 3cecfa6
Rename MiddleElementOfLinkedList.py to middle_element_of_linked_list.py
meSajied 3057ed7
changed "middle_element_of_linked_list.py" algorithm for taking input
meSajied ae6358c
Update middle_element_of_linked_list.py
meSajied f2b4111
Update middle_element_of_linked_list.py
meSajied 4675d7f
Update middle_element_of_linked_list.py
meSajied 5dbf5aa
Update middle_element_of_linked_list.py
meSajied 9a2aded
Update middle_element_of_linked_list.py
meSajied 8f55388
Update middle_element_of_linked_list.py
meSajied 8432de6
Update middle_element_of_linked_list.py
meSajied ddfe1d1
Update middle_element_of_linked_list.py
meSajied 5b7af7a
Update middle_element_of_linked_list.py
meSajied b80618d
Whack the trailing whitespace
cclauss 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
56 changes: 56 additions & 0 deletions
56
data_structures/linked_list/middle_element_of_linked_list.py
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
class Node: | ||
def __init__(self, data:int) -> int: | ||
self.data = data | ||
self.next = None | ||
|
||
class LinkedList: | ||
def __init__(self): | ||
self.head = None | ||
|
||
def push(self, new_data:int) -> int: | ||
new_node = Node(new_data) | ||
new_node.next = self.head | ||
self.head = new_node | ||
return self.head.data | ||
|
||
def middle_element(self) -> int: | ||
''' | ||
>>> link = LinkedList() | ||
>>> link.push(5) | ||
5 | ||
>>> link.push(6) | ||
6 | ||
>>> link.push(8) | ||
8 | ||
>>> link.push(8) | ||
8 | ||
>>> link.push(10) | ||
10 | ||
>>> link.push(12) | ||
12 | ||
>>> link.push(17) | ||
17 | ||
>>> link.push(7) | ||
7 | ||
>>> link.push(3) | ||
3 | ||
>>> link.middle_element() | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
10 | ||
>>> | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
''' | ||
slow_pointer = self.head | ||
fast_pointer = self.head | ||
if self.head is not None: | ||
while (fast_pointer is not None and fast_pointer.next is not None): | ||
fast_pointer = fast_pointer.next.next | ||
slow_pointer = slow_pointer.next | ||
return slow_pointer.data | ||
|
||
if __name__ == "__main__": | ||
link = LinkedList() | ||
N = int(input().strip()) | ||
|
||
for i in range(N): | ||
data = int(input().strip()) | ||
link.push(data) | ||
print(link.middle_element()) |
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.
Uh oh!
There was an error while loading. Please reload this page.