Skip to content

Commit 6ffbce2

Browse files
committed
Runtime: 45 ms (Top 13.51%) | Memory: 17.40 MB (Top 21.17%)
1 parent 74ece19 commit 6ffbce2

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed
Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1+
// Runtime: 45 ms (Top 13.51%) | Memory: 17.40 MB (Top 21.17%)
2+
13
class Solution:
2-
def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
3-
# basically we create two pointers
4-
# move one pointer extra fast
5-
# another pointer would be slow
6-
# when fast reaches end slow would be in mid
7-
slow = fast = head
8-
while fast and fast.next:
9-
slow = slow.next
10-
fast = fast.next.next
11-
return slow
4+
def middleNode(self, head):
5+
Iter, N = head, 0
6+
while Iter:
7+
Iter, N = Iter.next, N + 1
8+
for i in range(N//2):
9+
head = head.next
10+
return head

0 commit comments

Comments
 (0)