Skip to content

Update_middle_element_of_linked_list #3379

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

Closed
wants to merge 1 commit into from
Closed
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
51 changes: 51 additions & 0 deletions data_structures/linked_list/middle_element_of_linked_list.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#METHOD:1

class Node:
def __init__(self, data: int) -> int:
self.data = data
Expand Down Expand Up @@ -62,3 +64,52 @@ def middle_element(self) -> int:
data = int(input().strip())
link.push(data)
print(link.middle_element())



#METHOD:2

# Python program to find the middle of a
# given linked list

class Node:
def __init__(self, value):
self.data = value
self.next = None

class LinkedList:

def __init__(self):
self.head = None

# create Node and and make linked list
def push(self, new_data):
new_node = Node(new_data)
new_node.next = self.head
self.head = new_node

def printMiddle(self):
temp = self.head
count = 0

while self.head:

# only update when count is odd
if (count & 1):
temp = temp.next
self.head = self.head.next

# increment count in each iteration
count += 1

print(temp.data)

# Driver code
llist = LinkedList()
llist.push(1)
llist.push(20)
llist.push(100)
llist.push(15)
llist.push(35)
llist.printMiddle()