Skip to content

Commit 5598bcf

Browse files
committed
Time: 54 ms (14.68%), Space: 13.9 MB (91.10%) - LeetHub
1 parent 0c15830 commit 5598bcf

File tree

1 file changed

+9
-25
lines changed

1 file changed

+9
-25
lines changed
Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,15 @@
11
# Definition for singly-linked list.
2-
# class ListNode(object):
2+
# class ListNode:
33
# def __init__(self, val=0, next=None):
44
# self.val = val
55
# self.next = next
6-
class Solution(object):
7-
def middleNode(self, head):
8-
"""
9-
:type head: ListNode
10-
:rtype: ListNode
11-
"""
12-
node=head
13-
n=0
14-
while(head):
15-
n+=1
16-
head=head.next
17-
print(n)
18-
if(n%2==0):
19-
p=n//2
20-
while(p):
21-
node=node.next
22-
p-=1
23-
return node
24-
else:
25-
p=(n//2)+1
26-
while(p-1):
27-
node=node.next
28-
p-=1
29-
return node
6+
class Solution:
7+
def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
8+
slow=head
9+
fast=head
10+
while(fast and fast.next):
11+
slow=slow.next
12+
fast=fast.next.next
13+
return slow
3014

3115

0 commit comments

Comments
 (0)