-
Notifications
You must be signed in to change notification settings - Fork 0
/
876.py
37 lines (30 loc) · 907 Bytes
/
876.py
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
# Name: Middle of the Linked List
# Link: https://leetcode.com/problems/middle-of-the-linked-list/
# Method: Fast slow runner
# Time: O(n)
# Space: O(1)
# Difficulty: Easy
# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def middleNode(self, head: ListNode) -> ListNode:
slowboie = head
fastboye = head
while fastboye != None:
fastboye = fastboye.next
if fastboye:
slowboie = slowboie.next
fastboye = fastboye.next
else:
break
return slowboie.val
if __name__ == "__main__":
sol = Solution()
head = ListNode(3)
head.next = ListNode(4)
head.next.next = ListNode(5)
head.next.next.next = ListNode(6)
print(sol.middleNode(head))