[Leetcode] 160. Intersection of Two Linked Lists

No160. Intersecting Linked List

topic

Write a program to find the starting node where two singly linked lists intersect.

Such as the following two linked lists:

Insert picture description here

The intersection starts at node c1.

Example 1

Insert picture description here

  • Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
  • 输出:Reference of the node with value = 8
  • Input explanation: The value of the intersection node is 8 (note that if two linked lists intersect, it cannot be 0). Starting from the respective headers, linked list A is [4,1,8,4,5] and linked list B is [5,0,1,8,4,5]. In A, there are 2 nodes before the intersecting node; in B, there are 3 nodes before the intersecting node.

Example 2

Insert picture description here

  • Input: intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
  • 输出:Reference of the node with value = 2
  • Input explanation: The value of the intersection node is 2 (note that if two linked lists intersect, it cannot be 0). Starting from the respective headers, linked list A is [0,9,1,2,4], and linked list B is [3,2,4]. In A, there are 3 nodes before the intersecting node; in B, there are 1 node before the intersecting node.

Example 3

Insert picture description here

  • Input: intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
  • Output: null
  • Input explanation: Counting from the respective headers, linked list A is [2,6,4], and linked list B is [1,5]. Since these two linked lists do not intersect, intersectVal must be 0, and skipA and skipB can be arbitrary values.
    Explanation: The two linked lists do not intersect, so null is returned.

note

  • If there is no intersection between the two linked lists, null is returned.
  • After returning the result, the two linked lists must still maintain the original structure.
  • It can be assumed that there are no cycles in the entire linked list structure.
  • The program tries to satisfy O(n) time complexity, and only uses O(1) memory.

Ideas

  1. Calculate the length l1, l2 of the two linked lists respectively
  2. If l1 is not equal to l2, the tails of the two linked lists need to be aligned, and the pointer p of linked list 1 is moved to the corresponding position of linked list 2;
  3. Traverse, if the address is the same, then return to the current node;
  4. Return None at the end to indicate that although there is no intersecting node;

Problem-solving code (Python3)

class Solution:
    def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
        #返回列表长度
        def getLength(head):
            res = 0
            while head:
                res += 1
                head = head.next
            return res
        #移动指针
        def changePos(head,i):
            while i:
                head = head.next
                i -= 1
            return head
        if not headA or not headB:
            return None
        l1 = getLength(headA)
        l2 = getLength(headB)
        p = headA
        q = headB
        if l1 > l2:
            p = changePos(p,l1-l2)
        elif l2 > l1:
            q = changePos(q,l2-l1) 
        i = 0
        while i < min(l1,l2):
            if p==q:
                return p
            p = p.next
            q = q.next
            i += 1
        return None

Complexity analysis:

  • Time complexity O(n)
  • Space complexity O(1)

operation result:

Insert picture description here

Guess you like

Origin blog.csdn.net/Xiao_Spring/article/details/113760248