Monday, March 9, 2015

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:
A:          a1 → a2
                   ↘
                     c1 → c2 → c3
                   ↗            
B:     b1 → b2 → b3
begin to intersect at node c1.
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.
Solution1
1)如果两个链表的最后一个节点一样,那么说明两个链表一定有交点。
2)分别求出两个链表的长度,然后对长度长的链表向前移动:LenA - LenB,将两个链表进行对齐,之后一起遍历,直到找到第一个相同的节点。
/**
 * 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;
        
        int lenA = 0, lenB = 0;
        ListNode p1 = headA;
        ListNode p2 = headB;
        while(p1.next != null) {
            p1 = p1.next;
            lenA++;
        }
        while(p2.next != null) {
            p2 = p2.next;
            lenB++;
        }
        
        if(lenA > lenB) {
            int temp = lenA - lenB;
            while(temp > 0) {
                headA = headA.next;
                temp--;
            }
        }else {
            int temp = lenB - lenA;
            while(temp > 0) {
                headB = headB.next;
                temp--;
            }
        }
        
        while(headA != null && headB != null) {
            if(headA == headB)
                return headA;
            
            headA = headA.next;
            headB = headB.next;
        }
        
        return null;
    }
}
Solution2
Reference: https://github.com/leituo56/PracticeInJava/blob/master/src/org/leituo/leetcode/linkedList/IntersectionOf2LinkedLists.java
连成环。lanA + lanB = lanB + lanA, 所以 traverse A and then B, traverse B and then A. If they are same, return. If not, they will reach null at the same time. 

/**
 * 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;
        
        ListNode nodeA = headA;
        ListNode nodeB = headB;
        while(nodeA != null && nodeB != null && nodeA != nodeB) {
            nodeA = nodeA.next;
            nodeB = nodeB.next;
            if(nodeA == nodeB) {
                return nodeA;
            }
            if(nodeA == null) {
                nodeA = headB;
            }
            if(nodeB == null) {
                nodeB = headA;
            }
        }
        return nodeA;
    }
}

No comments:

Post a Comment