We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 74ece19 commit 6ffbce2Copy full SHA for 6ffbce2
scripts/algorithms/M/Middle of the Linked List/Middle of the Linked List.py
@@ -1,11 +1,10 @@
1
+// Runtime: 45 ms (Top 13.51%) | Memory: 17.40 MB (Top 21.17%)
2
+
3
class Solution:
- def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
- # 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
+ def middleNode(self, head):
+ Iter, N = head, 0
+ while Iter:
+ Iter, N = Iter.next, N + 1
+ for i in range(N//2):
+ head = head.next
+ return head
0 commit comments