class Solution:
    def middleNode(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        tmp = head
        while tmp and tmp.next:
            head = head.next
            tmp = tmp.next.next
        return head


if __name__ == '__main__':
    node_list = [7, 6, 5, 4, 3, 2, 1]
    h = list.ListNode_handle()
    # node = ListNode()

    for i in node_list:  # 循环向链表中加入Node
        h.add(i)
    # 头插法形成的链表顺序是1->2->3->4->5

    print('第一个结点值:', h.cur_node.val)
    print('第二个结点值:', h.cur_node.next.val)
    print('...')
    print("--" * 50)

    C = Solution()
    D = C.middleNode(h.cur_node)
    print('返回的第一个结点值:', D.val)
    print('返回值的类型:', type(D))
Exemple #2
0
        # Find the start of the loop
        fast = slow = headA
        while fast and fast.next:
            slow, fast = slow.next, fast.next.next
            if slow == fast:
                fast = headA
                while fast != slow:
                    slow, fast = slow.next, fast.next
                last.next = None
                return slow

        # No loop found
        last.next = None
        return None


if __name__ == '__main__':
    node_list1 = [9, 8, 7, 2, 1]
    node_list2 = [9, 8, 7, 4, 3, 2]
    l1 = list.ListNode_handle()
    l2 = list.ListNode_handle()
    for i in node_list1:
        l1.add(i)
    for i in node_list2:
        l2.add(i)
    A = Solution()
    B = A.getIntersectionNode(l1.cur_node, l2.cur_node)
    while B.next:
        print(B.val, end='')
        B = B.next
    print(B.val)