-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFinding middle element in a linked list.py
More file actions
54 lines (42 loc) · 1.09 KB
/
Finding middle element in a linked list.py
File metadata and controls
54 lines (42 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class Solution:
def findMid(self, head):
if head == None:
return None
slow = head
fast = head
while fast != None and fast.next != None:
slow = slow.next
fast = fast.next.next
return slow.data
class node:
def __init__(self):
self.data = None
self.next = None
class Linked_List:
def __init__(self):
self.head = None
self.tail = None
def insert(self, data):
if self.head == None:
self.head = node()
self.tail = self.head
self.head.data = data
else:
new_node = node()
new_node.data = data
new_node.next = None
self.tail.next = new_node
self.tail = self.tail.next
def printlist(head):
temp = head
while temp is not None:
print(temp.data, end=" ")
temp = temp.next
print("")
list1 = Linked_List()
n = int(input())
values = list(map(int, input().strip().split()))
for i in values:
list1.insert(i)
ob = Solution()
print(ob.findMid(list1.head))