File tree Expand file tree Collapse file tree 1 file changed +9
-25
lines changed
876-middle-of-the-linked-list Expand file tree Collapse file tree 1 file changed +9
-25
lines changed Original file line number Diff line number Diff line change 1
1
# Definition for singly-linked list.
2
- # class ListNode(object) :
2
+ # class ListNode:
3
3
# def __init__(self, val=0, next=None):
4
4
# self.val = val
5
5
# 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
30
14
31
15
You can’t perform that action at this time.
0 commit comments