[Offer] [52] [a first common node of the two lists]

Title Description

  Two input lists, find their first common node. Figure 6 is the common node:

Cattle brush off questions address network

Ideas analysis

  If there are two lists common node, the common node appear at the end of the two lists. If we start comparing the two lists from the tail forward, then the last one and the same node we are looking for nodes.

  1. To solve this problem: are the two lists of nodes into two stacks, the two lists so that the tail node on top of the stack two stacks, then compare the two stack nodes are the same. If so, put the top of the stack pop stack is then compared to the next, until you find the last one and the same node.
  2. First traverse two lists to get their length, which will be able to know the list is long, and longer than the list of short list a few more nodes. When the second pass, and go over a long list of several steps, and then traversed simultaneously on two lists, the first one and the same node is found their first common node.

Test Case

  1. Functional test: two inputs have a common node list: a first common node in the middle of the list, the first common node at the end of the list, the first node is a common node of the head of the linked list; no common input two lists node.
  2. Special input test: an input node of the list head pointer is nullptr.

Java code

public class Offer052 {
    public static void main(String[] args) {
        test1();
        test2();
        test3();
        
    }

    public static ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
         return Solution1(pHead1,pHead2);
    }

    /**
     * 利用长度关系
     * @param pHead1
     * @param pHead2
     * @return
     */
    private static ListNode Solution1(ListNode pHead1, ListNode pHead2) {
        int listLength1 = getListLength(pHead1);
        int listLength2 = getListLength(pHead2);
        int dif = listLength1-listLength2;
        ListNode longList = pHead1;
        ListNode shortList = pHead2;
        if(listLength1<listLength2) {
            longList = pHead2;
            shortList = pHead1;
            dif = listLength2-listLength1;
        }
        for(int i=0;i<dif;i++) {
            longList = longList.next;
        }
        while(longList!=null && shortList!=null && longList!=shortList) {
            longList = longList.next;
            shortList = shortList.next;
        }
        ListNode firstCommonFirst = longList;
        
        return firstCommonFirst;
    }
    
    

    private static int getListLength(ListNode pHead1) {
        int length = 0;
        while(pHead1!=null) {
            length++;
            pHead1 = pHead1.next;
        }
        return length;
    }

    private static void test1() {

    }

    private static void test2() {

    }
    private static void test3() {

    }
}

Code link

Offer to prove safety codes -Java

Guess you like

Origin www.cnblogs.com/haoworld/p/offer52-liang-ge-lian-biao-de-di-yi-ge-gong-gong-j.html