Likou 160. Intersecting linked list double pointer python

160. Intersecting Linked
Lists Given you two singly linked lists headA and headB, please find and return the starting node where the two singly linked lists intersect. Returns null if the two linked lists do not have intersecting nodes.

The illustration shows that the two linked lists intersect at node c1:


The title data ensures that there are no cycles in the entire chain structure.

Note that the linked list must retain its original structure after the function returns the result.

Custom Assessments:

The input of the evaluation system is as follows (this input is not applicable to the program you designed):

  • intersectVal - the value of the starting node of the intersection. If there is no intersecting node, this value is 0
  • listA - the first linked list
  • listB - the second linked list
  • skipA - the number of nodes to skip to the cross node in listA (starting from the head node)
  • skipB - the number of nodes to skip to the cross node in listB (starting from the head node)

The profiling system will create a chained data structure from these inputs and pass the two head nodes, headA and headB, to your program. If the program returns the intersection node correctly, then your solution will be considered the correct answer.
Problem solving idea
An interesting question. The first thing you can definitely think of is to store the traversed nodes. The method is similar to the circular linked list of question 141, but it requires only O(1) memory to do it. Although I know that I need to use double pointers, I didn't think how to traverse it.

After reading the official solution, it is really whimsical. The p_a pointer traverses the list A first, and after A traverses it, it traverses B; the p_b pointer traverses B first, and after B traverses, it traverses A. When p_a traverses B and p_b traverses A, if there are two pointers of intersecting nodes, they can be encountered here. Here's a little explanation:

Because the two lists a and b can be divided into two sections:
a is divided into the section where a does not intersect with b + the section where a and b intersect;
b is divided into the section where b does not intersect with a + the section where a and b intersect

When the two pointers go to the intersection node:
p_a walks through the section where a does not intersect with b + a, b intersects the section + b does not intersect with a
p_b walks through the section where b does not intersect with a + a, The segment where b intersects + the segment where a does not intersect with b

It can be seen that the distance traveled by the two pointers is the same, so they can arrive at the same time.

The specific implementation is as follows:


class Solution(object):
    def getIntersectionNode(self, headA, headB):
        """
        :type head1, head1: ListNode
        :rtype: ListNode
        """
        head1 = headA
        head2 = headB
        while head1 != head2:
            head1 = head1.next if head1 else headB
            head2 = head2.next if head2 else headA
        return head1

———————————————
Copyright statement: This article is an original article by CSDN blogger "KeEN, X", following the CC 4.0 BY-SA copyright agreement, please attach the original source link and this statement.
Original link: https://blog.csdn.net/KeEN_Xwh/article/details/108169312

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324340880&siteId=291194637