Analyzing two-way linked list intersect

Divided into three cases:
the first case: a linked list has a ring, the ring is not a list, it is unlikely that two lists intersect
the second case: two lists do not ring
the third case: there are two lists ring

public class  FindFirstIntersectNode {

    public static class Node {
        public int value;
        public Node next;

        public Node(int data) {
            this.value = data;
        }
    }

    /**
     * Divided into three cases:
     * The first case: a linked list has a ring, the ring is not a list, it is impossible that two lists intersect.
     * The second case: two lists did not ring,
     * The third case: two lists are rings,
     */
    public static Node getIntersectNode(Node head1, Node head2) {
        if (head1 == null || head2 == null) {
            return null;
        }
        Node loop1 = getLoopNode(head1);
        Node loop2 = getLoopNode(head2);
        
        // two chain acyclic intersects 
        IF (Loop1 == null && Loop2 == null ) {
             return noloop (head1, head2 The);
        }
        
        // two looped chain intersects 
        IF (Loop1 =! Null && Loop2 =! Null ) {
             return bothLoop (head1, Loop1, head2 The, Loop2);
        }
        return null;
    }

    
    /**
     * Determining whether a chain ring, a chain looped back to the first node of the ring, the ring without or null
     * @param head
     * @return
     * / 
    Public  static the Node getLoopNode (the Node head) {
        // first determined speed of the pointer meeting point cross 
        the Node = FAST head;
        Node slow = head;
        Node cross = null;
        while (fast.next != null && fast.next.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (slow == fast) {
                cross = fast;
                break;
            }
        }
        // starting from the meeting point and the head of the list, each time a mobile node, they met the entrance point is ring 
        the Node Start = head;
         the while (! = Start null ! && Cross = null ) {
             IF (Start == Cross) {
                 return Cross;
            }
            start = start.next;
            cross = cross.next;
        }
        return null;
    }

    
    /**
     * Two chain acyclic intersect, the intersection returns the first intersection node, or null disjoint
     * @param head1
     * @Param head2
     * @return
     */
    public static Node noLoop(Node head1, Node head2) {
        if (head1 == null || head2 == null) {
            return null;
        }
        Node cur1 = head1;
        int  len1 = 1;
        Node cur2 = head2;
        int  len2 = 1;
        while(cur1.next != null) {
            len1 ++ ;
            cur1 = cur1.next;
        }
        while(cur2.next != null) {
            len2++;
            cur2 = cur2.next;
        }
        // the last node is not the same, two disjoint lists 
        IF (Cur1 =! CUR2) {
             return  null ;
        }
        // First always points to the beginning of a long chain, second chain always points to the beginning of the short, diff represents the difference in length of two lists 
        the Node First = null ;
        SECOND the Node = null ;
         int the diff = 0 ;
         IF (LEN1> LEN2) { // list 2 lists longer than 1, 
            First = head1;
            second = head2;
            diff = len1 -len2;
        }else {
            first = head2;
            second = head1;
            diff = len2 - len1;
        }
        // premise diff is not zero, go a long chain diff 
        the while (diff> 0 ) {
            first = first.next;
            diff--;
        }
        // First and second take Meanwhile, if the same, showing the addition of 
        the while (First! = Second) {
            first =first.next;
            second = second.next;
        }
        return first;
    }

    /**
     * There are two chain rings intersect, the intersection returns the first intersection node, or null disjoint
     * @Param head1 node of the first head of the list
     * @Param loop1 first list of ingress node
     * @Param head2 second linked list head node
     * @Param loop2 ingress point of the second list
     * @return
     */
    public static Node bothLoop(Node head1, Node loop1, Node head2, Node loop2) {
        Node cur1 = null;
        CUR2 the Node = null ;
         IF (== Loop1 Loop2) { // corresponds to a case where two and, the list is determined two acyclic intersect the same procedure, except the end node into a Loop1 
            Cur1 = head1;
            cur2 = head2;
            int n = 0;
            while (cur1 != loop1) {
                n++;
                cur1 = cur1.next;
            }
            while (cur2 != loop2) {
                n--;
                cur2 = cur2.next;
            }
            cur1 = n > 0 ? head1 : head2;
            cur2 = cur1 == head1 ? head2 : head1;
            n = Math.abs(n);
            while (n != 0) {
                n--;
                cur1 = cur1.next;
            }
            while (cur1 != cur2) {
                cur1 = cur1.next;
                cur2 = cur2.next;
            }
            return cur1;
        } The else {    // corresponding to the case where three and four, in which case a three-phase, the case of a four disjoint 
            Cur1 = loop1.next;
             the while (! = Cur1 Loop1) {
                 IF (Cur1 == Loop2) {
                     return Loop1;
                }
                cur1 = cur1.next;
            }
            return null;
        }
    }

    public static void main(String[] args) {
        // 1->2->3->4->5->6->7->null
        Node head1 = new Node(1);
        head1.next = new Node(2);
        head1.next.next = new Node(3);
        head1.next.next.next = new Node(4);
        head1.next.next.next.next = new Node(5);
        head1.next.next.next.next.next = new Node(6);
        head1.next.next.next.next.next.next = new Node(7);

        // 0->9->8->6->7->null
        Node head2 = new Node(0);
        head2.next = new Node(9);
        head2.next.next = new Node(8);
        head2.next.next.next = head1.next.next.next.next.next; // 8->6
        System.out.println(getIntersectNode(head1, head2).value);

        // 1->2->3->4->5->6->7->4...
        head1 = new Node(1);
        head1.next = new Node(2);
        head1.next.next = new Node(3);
        head1.next.next.next = new Node(4);
        head1.next.next.next.next = new Node(5);
        head1.next.next.next.next.next = new Node(6);
        head1.next.next.next.next.next.next = new Node(7);
        head1.next.next.next.next.next.next.next = head1.next.next.next; // 7->4

        // 0->9->8->2...
        head2 = new Node(0);
        head2.next = new Node(9);
        head2.next.next = new Node(8);
        head2.next.next.next = head1.next; // 8->2
        System.out.println(getIntersectNode(head1, head2).value);

        // 0->9->8->6->4->5->6..
        head2 = new Node(0);
        head2.next = new Node(9);
        head2.next.next = new Node(8);
        head2.next.next.next = head1.next.next.next.next.next; // 8->6
        System.out.println(getIntersectNode(head1, head2).value);

    }

}

 

A ring intersects two-way linked list example of FIG.

 

 

Acyclic two-way linked list example of FIG intersect

 

Guess you like

Origin www.cnblogs.com/moris5013/p/11647951.html