Problem Description:
Given a non-empty list with a single head of the first node, an intermediate node list returned.
If there are two intermediate nodes, the second intermediate node is returned.
Example 1:
Input: [1,2,3,4,5]
Output: node 3 (SEQ form: [3,4,5]) in the list of
returned node is 3. (This evaluation system is expression of the sequence node [3,4,5]).
Note that, we return an object of type ListNode ANS, so that:
. Ans.val. 3 =, = ans.next.val. 4, ans.next.next.val =. 5, and ans.next.next.next = NULL
Example 2:
Input: [1,2,3,4,5,6]
Output: this list node 4 (SEQ form: [4,5,6])
Since the list has two intermediate nodes, values of 3 and 4, we return to the second node.
prompt:
Given the list of nodes is between 1 and 100.
Core: a slow pointer each time step, a quick pointer each take two steps when the pointer quickly went to the tail, then slow pointer node is an intermediate node.
# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def middleNode(self, head: ListNode) -> ListNode: slow = head fast = head while fast and fast.next: slow = slow.next fast = fast.next.next return slow
result: