-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddle_node.py
41 lines (29 loc) · 1.08 KB
/
middle_node.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from typing import Optional
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
"""
Given the head of a singly linked list, return the middle node of the linked list.
If there are two middle nodes, return the second middle node.
"""
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
return slow
# Time complexity: O(n)
# Space complexity: O(1)
import unittest
class TestSolution(unittest.TestCase):
def setUp(self):
self.solution = Solution()
def test_middleNode(self):
head = ListNode(1, ListNode(2, ListNode(3, ListNode(4, ListNode(5)))))
self.assertEqual(self.solution.middleNode(head).val, 3)
head = ListNode(1, ListNode(2, ListNode(3, ListNode(4, ListNode(5, ListNode(6))))))
self.assertEqual(self.solution.middleNode(head).val, 4)
if __name__ == '__main__':
unittest.main()