Skip to content

Commit 85a9ce2

Browse files
committed
Added Python solution for "Middle of the Linked List"
1 parent a0df862 commit 85a9ce2

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

Python/Middle of the Linked List.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from typing import Optional
2+
3+
4+
class ListNode:
5+
def __init__(self, val=0, next=None):
6+
self.val = val
7+
self.next = next
8+
9+
10+
class Solution:
11+
"""
12+
Time: O(n)
13+
Memory: O(1)
14+
"""
15+
16+
def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
17+
slow = fast = head
18+
19+
while fast and fast.next:
20+
slow = slow.next
21+
fast = fast.next.next
22+
23+
return slow
0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)