1+ """
2+ LeetCode 876 - Middle of the Linked List
3+
4+ Problem:
5+ Given the head of a singly linked list, return the middle node.
6+
7+ If there are two middle nodes, return the second middle node.
8+
9+ Example 1:
10+ Input: head = [1,2,3,4,5]
11+ Output: [3,4,5]
12+
13+ Example 2:
14+ Input: head = [1,2,3,4,5,6]
15+ Output: [4,5,6]
16+
17+ ----------------------------------------------------------
18+ Approach 1: Counting Nodes (Two Pass)
19+ ----------------------------------------------------------
20+
21+ 1. Count the total number of nodes.
22+ 2. Find middle index:
23+ middle = count // 2
24+ 3. Traverse again to the middle node.
25+ 4. Return that node.
26+
27+ Time Complexity: O(n)
28+ Space Complexity: O(1)
29+
30+ ----------------------------------------------------------
31+ Approach 2: Fast & Slow Pointer (Optimal)
32+ ----------------------------------------------------------
33+
34+ 1. Initialize:
35+ slow = head
36+ fast = head
37+
38+ 2. Move:
39+ slow -> 1 step
40+ fast -> 2 steps
41+
42+ 3. When fast reaches the end:
43+ slow will be at the middle.
44+
45+ 4. Return slow.
46+
47+ Time Complexity: O(n)
48+ Space Complexity: O(1)
49+ """
50+
51+ # Definition for singly-linked list.
52+ # class ListNode:
53+ # def __init__(self, val=0, next=None):
54+ # self.val = val
55+ # self.next = next
56+
57+
58+ class Solution :
59+
60+ # --------------------------------------------------
61+ # Approach 1: Counting Method
62+ # --------------------------------------------------
63+ def middleNode_Counting (self , head : Optional [ListNode ]) -> Optional [ListNode ]:
64+
65+ count = 0
66+ curr = head
67+
68+ while curr :
69+ count += 1
70+ curr = curr .next
71+
72+ middle = count // 2
73+
74+ curr = head
75+
76+ for _ in range (middle ):
77+ curr = curr .next
78+
79+ return curr
80+
81+ # --------------------------------------------------
82+ # Approach 2: Fast & Slow Pointer (Optimal)
83+ # --------------------------------------------------
84+ def middleNode (self , head : Optional [ListNode ]) -> Optional [ListNode ]:
85+
86+ slow = head
87+ fast = head
88+
89+ while fast and fast .next :
90+ slow = slow .next
91+ fast = fast .next .next
92+
93+ return slow
94+
95+
96+ """
97+ =================================================
98+ Dry Run
99+ =================================================
100+
101+ Input:
102+ 1 -> 2 -> 3 -> 4 -> 5
103+
104+ Initial:
105+ slow = 1
106+ fast = 1
107+
108+ Iteration 1:
109+ slow = 2
110+ fast = 3
111+
112+ Iteration 2:
113+ slow = 3
114+ fast = 5
115+
116+ Loop Ends
117+
118+ Return slow = 3
119+
120+ Output:
121+ 3 -> 4 -> 5
122+
123+ =================================================
124+ Even Length Example
125+ =================================================
126+
127+ Input:
128+ 1 -> 2 -> 3 -> 4 -> 5 -> 6
129+
130+ Initial:
131+ slow = 1
132+ fast = 1
133+
134+ Iteration 1:
135+ slow = 2
136+ fast = 3
137+
138+ Iteration 2:
139+ slow = 3
140+ fast = 5
141+
142+ Iteration 3:
143+ slow = 4
144+ fast = None
145+
146+ Return slow = 4
147+
148+ Output:
149+ 4 -> 5 -> 6
150+
151+ Note:
152+ For even-length lists, the problem asks us to
153+ return the SECOND middle node.
154+
155+ =================================================
156+ Complexity Analysis
157+ =================================================
158+
159+ Approach Time Space
160+ --------------------------------------
161+ Counting Method O(n) O(1)
162+ Fast & Slow Pointer O(n) O(1)
163+
164+ Recommended:
165+ ✔ Fast & Slow Pointer
166+
167+ Pattern:
168+ ✔ Tortoise and Hare (Fast & Slow Pointer)
169+ """
0 commit comments