-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path876.py
54 lines (49 loc) · 1.58 KB
/
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
__________________________________________________________________________________________________
sample 20 ms submission
# 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:
if(head.next == None):
return head
slowPtr = head
fastPtr = head.next
while(fastPtr.next!=None and fastPtr.next.next!=None):
fastPtr = fastPtr.next.next
slowPtr = slowPtr.next
return slowPtr.next
__________________________________________________________________________________________________
sample 13056 kb submission
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
import math
class Solution:
def middleNode(self, head: ListNode) -> ListNode:
target = None
length = Solution.getLength(head)
if length % 2 == 0:
target = (length/2) + 1
else:
target = math.ceil(length/2)
n = 1
node = head
while True:
if n == target:
return node
node = node.next
n += 1
@staticmethod
def getLength(head: ListNode) -> int:
n = 1
node = head
while node.next is not None:
node = node.next
n += 1
return n
__________________________________________________________________________________________________