Leetcode topic 160. intersection list (simple)

Title Description

Write a program to find the starting node two single list intersect.

The following two lists:

Beginning at the intersection node c1.

Input: intersectVal = 8, listA = [ 4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3 
Output: Reference of the node with value = 8 
input interpretation: intersection node is 8 (note, if the two intersecting listing can not be 0). From the respective header start date, list A is [4,1,8,4,5], B is a chain [5,0,1,8,4,5]. In A, the first intersection node has two nodes; in B, there are three nodes before the intersection node.

Input: intersectVal = 2, listA = [ 0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 
Output: Reference of the node with value = 2 
Input interpretation: intersection node is 2 (note that, if it can not intersect the two lists is 0). From the respective header start date, list A is [0,9,1,2,4], list B is [3,2,4]. In A, the first intersection node has three nodes; in B, there is an intersection node before the node.

Input: intersectVal = 0, listA = [ 2,6,4], listB = [1,5], skipA = 3, skipB = 2 
Output: null 
input interpretation: from the respective header start date, list A is [ 2,6,4], B is a chain [1,5]. Since these two lists do not intersect, it intersectVal must be 0, and the skipA skipB may be any value. 
Explanation: This two lists do not intersect, and therefore returns null.

note:

If there is no intersection of two lists, returns null.
After returning to the results, two lists must still maintain the original structure.
The entire list may be assumed that there is no cycle structure.
Try to meet program (n) time complexity of O, and only O (1) memory.

Analysis of ideas:

According to the meaning of the title 
if the two lists intersect, then the length of the intersection point is after the same 

thing we need to do is to make two lists from the same location at the same distance from the end of traversing. The position of the first node is only the position of a short list. 
To this end, we must eliminate the difference between the length of two lists: 

a pointer pointing A list pA, pB pointer B point to the list, in turn traversed back 
if pA to end, continue to traverse the pA = headB 
If pB to the end, to continue the pB = headA traversal 
when a long list head pointer to the linked list shorter, the length difference is eliminated 
thus, only need to traverse the shortest list to find two positions

In plain English it is: I go my way, walk your way, you go your way, walk my way, then we're walking away on the same, the same pace, then certainly we two two Road intersection encounter, and together shoulder to shoulder coming to an end.

Code:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
       public static ListNode getIntersectionNode(ListNode headA, ListNode headB) {

        if (headA == null || headB == null) {
            return null;
        }

        //双指针;pNode指向A链表,qNode指向B链表
        ListNode pNode = headA;
        ListNode qNode = headB;
        while (pNode != qNode) {
            pNode = pNode == null ? headB : pNode.next;
            qNode = qNode == null? Head: qNode.next; 
        } 
        Return pNode; 
    } 
}

Space complexity: O (1)
time complexity: O (n)

Guess you like

Origin www.cnblogs.com/ysw-go/p/11867313.html