【Leetcode】 Intersection of two linked list

来源:互联网 发布:@@error mysql 编辑:程序博客网 时间:2024/05/22 19:13



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.


【解法】

遍历两个list L1 L2


L1+L2 == L2 +L1

实现细节:

两个p1 ,p2的衔接点要判断,只能一次,所以要用boolean,或者数量来控制。

my solituon:

/** * 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) {ListNode l1 = headA;ListNode p1 = headA;ListNode l2 = headB;ListNode p2 = headB;boolean flag1 = true;boolean flag2 = true;while(p1!=null && p2 != null){if (p1 == p2) return p2;else{if(p1.next==null && flag1 == true) {p1 = l2;flag1 = false;if(p2.next == null && flag2 == true) {p2 = l1;flag2 = false;continue;}p2 = p2.next;continue;}if(p2.next==null && flag2 ==true) {p2 = l1;flag2 = false;if(p1.next==null && flag1 == true) {p1 = l2;flag1 =false;continue;}p1 = p1.next;continue;}p1 = p1.next;p2 = p2.next;}}return null;    }}



【官方答案】

There are many solutions to this problem:

  • Brute-force solution (O(mn) running time, O(1) memory):

    For each node ai in list A, traverse the entire list B and check if any node in list B coincides with ai.

  • Hashset solution (O(n+m) running time, O(n) or O(m) memory):

    Traverse list A and store the address / reference to each node in a hash set. Then check every node bi in list B: if bi appears in the hash set, then bi is the intersection node.

  • Two pointer solution (O(n+m) running time, O(1) memory):
    • Maintain two pointers pA and pB initialized at the head of A and B, respectively. Then let them both traverse through the lists, one node at a time.
    • When pA reaches the end of a list, then redirect it to the head of B (yes, B, that's right.); similarly when pB reaches the end of a list, redirect it the head of A.
    • If at any point pA meets pB, then pA/pB is the intersection node.
    • To see why the above trick would work, consider the following two lists: A = {1,3,5,7,9,11} and B = {2,4,9,11}, which are intersected at node '9'. Since B.length (=4) < A.length (=6), pB would reach the end of the merged list first, because pB traverses exactly 2 nodes less than pA does. By redirecting pB to head A, and pA to head B, we now ask pB to travel exactly 2 more nodes than pA would. So in the second iteration, they are guaranteed to reach the intersection node at the same time.
    • If two lists have intersection, then their last nodes must be the same one. So when pA/pB reaches the end of a list, record the last element of A/B respectively. If the two last elements are not the same one, then the two lists have no intersections.


public class Solution {public ListNode getIntersectionNode(ListNode headA, ListNode headB) {    ListNode A = headA;    ListNode B = headB;    int times = 0;    while(times<=1){        if(A==B)          return A;        if(A==null){            A=headB;            times++;        }        else          A=A.next;        if(B==null)          B=headA;        else          B=B.next;                  }    return null;    }}



hashset:

public class Solution {    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {        HashSet<ListNode> set = new HashSet<ListNode>();        while(headA!=null||headB!=null){            if(headA!=null) {                if(set.contains(headA)){                    return headA;                } else {                    set.add(headA);                }                headA=headA.next;            }            if(headB!=null) {                if(set.contains(headB)) {                    return headB;                } else {                    set.add(headB);                }                headB=headB.next;            }        }        return null;    }}



0 0
原创粉丝点击