-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddle_of_a_linked_list.py
48 lines (43 loc) · 1.34 KB
/
middle_of_a_linked_list.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
38
39
40
41
42
43
44
45
46
47
48
#traverse through the whole list first time and count how many nodes are there
#then traverse only till the mid, total / 2, and return
#O(n) + O(n) ~ O(n) || O(1)
'''
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
total = 0
current = head
while current!= None:
total += 1
current = current.next
mid = (total // 2) + 1
count = 1
while head!= None:
# print(head)
if count == mid:
return head
count += 1
head = head.next
'''
#When traversing the list with a pointer slow, make another pointer fast that traverses twice as fast.
#When fast reaches the end of the list, slow must be in the middle.
#o(n) || O(1)
'''
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
slow = head
fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
return slow
'''