# 160 Intersection of Two Linked Lists

Write a program to find the node at which the intersection of two singly linked lists begins.

For example, the following two linked lists:

begin to intersect at node c1.

Example 1:

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 Explanation: The intersected node's value is 8 (note that this must not be 0 if the two lists intersect). From the head of A, it reads as [4,1,8,4,5]. From the head of B, it reads as [5,0,1,8,4,5]. There are 2 nodes before the intersected node in A; There are 3 nodes before the intersected node in B.

Example 2:

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 Explanation: The intersected node's value is 2 (note that this must not be 0 if the two lists intersect). From the head of A, it reads as [0,9,1,2,4]. From the head of B, it reads as [3,2,4]. There are 3 nodes before the intersected node in A; There are 1 node before the intersected node in B.

Example 3:

Input: intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
Output: null
Input Explanation: From the head of A, it reads as [2,6,4]. From the head of B, it reads as [1,5]. Since the two lists do not intersect, intersectVal must be 0, while skipA and skipB can be arbitrary values.
Explanation: The two lists do not intersect, so return null.

Notes:

  • If the two linked lists have no intersection at all, return null.

  • The linked lists must retain their original structure after the function returns.

  • You may assume there are no cycles anywhere in the entire linked structure.

  • Your code should preferably run in O(n) time and use only O(1) memory.

題目解析:

這題是要找到相錯的點。

解題思路:

利用了鍊錶無環這個限定條件,即便兩個鍊錶長度不同,但是同時使用兩個指針分別跑完兩個鍊錶,總是會在交叉點相遇,也就是說第一個指針跑了x+n+y,第二個指針跑了y+n+x,然後二者相遇。代碼入下

a + b = b+ a

以a [1,2,3,4]

b[9,5,6,7,4]

a 1,2,3,4 ,9,5,6,7,4

b 9,5,6,7,4 1,2,3,4

程式碼:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if(headA == null || headB == null){
            return null;
        }
        /*
        headA [4,1,8,4,5]
        headB [5,0,1,8,4,5]
        4
        a : [4,1,8,4,5] [5  ,0,1,8,4,5]
        b : [5,0,1,8,4,  5] [4,1,8,4,5]
        */
        ListNode a = headA;
        ListNode b = headB;
        // int count = 0;
        while(b != a){
            // if(a != b){
            //     count++;
            //     if(a != null){
            //         System.out.println("start ,  a.val =" + a.val + " , c = " + count);
            //     }
            //      if(b != null){
            //        System.out.println("start ,  b.val =" + b.val + " , c = " + count);
            //     }
            //     System.out.println("\n");
            // }
            b = b == null? headA: b.next;
            a = a == null? headB: a.next;
        }
        
        // System.out.println(" , a.val =" + a.val);
        return a;
    }
}

另外可以用hashset來解題,但不符合O(1) memory

程式碼:

/*
input:
8
[4,1,8,4,5]
[5,0,1,8,4,5]
2
3
*/

public ListNode MegeNode(ListNode n1, ListNode n2){ 
        // define hashset 
        HashSet<ListNode> hs = new HashSet<ListNode>(); 
        while (n1 != null) {
            System.out.println("val = " + n1.val + " hashCoce " + n1.hashCode() );
            hs.add(n1);  
            n1 = n1.next; 
        } 
        System.out.println("\n");
        while (n2 != null) { 
            if (hs.contains(n2)) { 
                
                return n2; 
            } 
            System.out.println("val = " + n2.val + " hashCoce " + n2.hashCode() );
            n2 = n2.next; 
        } 
        return null; 
    } 
    /*
    output
val = 4 hashCoce 895328852
val = 1 hashCoce 1304836502
val = 8 hashCoce 225534817
val = 4 hashCoce 1878246837
val = 5 hashCoce 929338653


val = 5 hashCoce 1259475182
val = 0 hashCoce 1300109446
val = 1 hashCoce 1020371697
    */

hashset 的contains(Object o) 方法是比對與Hashset裡的物件是否與o相同,而物件傳是是參考位址,所以在list1 有val = 1 node ,而list2同時也有val =1的node但仍是不同的物件,這裡是印出記憶體位置來確認。

java裡的"=="是比對值是否相同,但java裡的物件存是參考位址,所以在list1 有val = 1的node ,而list2同時也有val =1的node但仍是不同的物件。 list1 1304836502 != list2 1020371697

參考連結:

1 geekOfgeek

2 博客

3 博客

4 博客

Last updated

Was this helpful?