File tree 1 file changed +47
-0
lines changed
1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change
1
+ """"
2
+ 876. Middle of the Linked List
3
+
4
+ Given the head of a singly linked list, return the middle node of the linked list.
5
+
6
+ If there are two middle nodes, return the second middle node."""
7
+
8
+ #Brute-force approach-1
9
+
10
+
11
+ # Definition for singly-linked list.
12
+ # class ListNode:
13
+ # def __init__(self, val=0, next=None):
14
+ # self.val = val
15
+ # self.next = next
16
+ class Solution :
17
+ def middleNode (self , head : Optional [ListNode ]) -> Optional [ListNode ]:
18
+ curr = head
19
+ count = 1
20
+
21
+ while curr .next :
22
+ curr = curr .next
23
+ count += 1
24
+
25
+ curr = head
26
+ if count % 2 == 0 :
27
+ for count in range (count ,(count // 2 ),- 1 ):
28
+ curr = curr .next
29
+
30
+
31
+ else :
32
+ for count in range (count ,(count // 2 )+ 1 ,- 1 ):
33
+ curr = curr .next
34
+
35
+
36
+ head = curr
37
+ return head
38
+
39
+
40
+
41
+
42
+
43
+
44
+
45
+
46
+
47
+
You can’t perform that action at this time.
0 commit comments