【剑指Offer学习】【面试题37:两个链表的第一个公共结点】

题目:输入两个链表,找出它们的第一个公共结点。


链表结点定义

    /**
     * 链表结点类
     */
    private static class ListNode {
        int val;
        ListNode next;

        public ListNode() {

        }

        public ListNode(int val) {
            this.val = val;
        }

        @Override
        public String toString() {
            return val + "";
        }
    }

解题思路:

第一种:直接法
  在第一个链表上顺序遍历每一个结点,每遍历到一个结点的时候,在第二个链表上顺序遍历每一个结点。假设在第二个链表上有一个结点和第一个链表上的结点一样,说明两个链表在这个结点上重合。于是就找到了它们的公共结点。假设第一个链表的长度为m,第二个链表的长度为n,显然该方法的时间复杂度是O(mn) 。

另外一种:使用栈
  所以两个有公共结点而部分重舍的链衰。拓扑形状看起来像一个Y。 而不可能像X(如图5.3 所看到的)。

这里写图片描写叙述

  经过分析我们发现,假设两个链表有公共结点。那么公共结点出如今两个链表的尾部。假设我们从两个链衰的尾部開始往前比較。最后一个同样的结点就是我们要找的结点。


  在上述思路中,我们须要用两个辅助钱。假设链表的长度分别为m 和n,那么空间复杂度是O(m+n)。

这样的思路的时间复杂度也是O(m+n)。和最開始的蛮力法相比,时间效率得到了提高。相当于是用空间消耗换取了时间效率。

第三种:先行法
  在图5.3 的两个链表中,我们能够先遍历一次得到它们的长度分别为5 和4, 也就是较长的链表与较短的链表相比多一个结点。

第二次先在长的链表上走1 步。到达结点2。接下来分别从结点2 和结点4 出发同一时候遍历两个结点, 直到找到它们第一个同样的结点6,这就是我们想要的结果。
  第三种思路和另外一种思路相比。时间复杂度都是O(m+啡, 但我们不再须要辅助的拢,因此提高了空间效率。

本题採用第三种解法

代码实现

public class Test37 {
    /**
     * 链表结点类
     */
    private static class ListNode {
        int val;
        ListNode next;

        public ListNode() {

        }

        public ListNode(int val) {
            this.val = val;
        }

        @Override
        public String toString() {
            return val + "";
        }
    }

    /**
     * 找两个结点的第一个公共结点。假设没有找到返回null,方法比較好,考虑了两个链表中有null的情况
     *
     * @param head1 第一个链表
     * @param head2 第二个链表
     * @return 找到的公共结点。没有返回null
     */
    public static ListNode findFirstCommonNode(ListNode head1, ListNode head2) {
        int length1 = getListLength(head1);
        int length2 = getListLength(head2);

        int diff = length1 - length2;
        ListNode longListHead = head1;
        ListNode shortListHead = head2;

        if (diff < 0) {
            longListHead = head2;
            shortListHead = head1;
            diff = length2 - length1;
        }

        for (int i = 0; i < diff; i++) {
            longListHead = longListHead.next;
        }

        while (longListHead != null && shortListHead != null && longListHead != shortListHead) {
            longListHead = longListHead.next;
            shortListHead = shortListHead.next;
        }

        // 返回第一个同样的公共结点,假设没有返回null
        return longListHead;
    }

    private static int getListLength(ListNode head) {
        int result = 0;
        while (head != null) {
            result++;
            head = head.next;
        }

        return result;
    }

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

    private static void test1() {
        // 第一个公共结点在链表中间
        // 1 - 2 - 3 
        //            6 - 7
        //     4 - 5 /
        ListNode n1 = new ListNode(1);
        ListNode n2 = new ListNode(2);
        ListNode n3 = new ListNode(3);
        ListNode n4 = new ListNode(4);
        ListNode n5 = new ListNode(5);
        ListNode n6 = new ListNode(6);
        ListNode n7 = new ListNode(7);

        n1.next = n2;
        n2.next = n3;
        n3.next = n6;
        n6.next = n7;

        n4.next = n5;
        n5.next = n6;

        System.out.println(findFirstCommonNode(n1, n4)); // 6
    }


    private static void test2() {
        // 没有公共结点
        // 1 - 2 - 3 - 4
        //
        // 5 - 6 - 7
        ListNode n1 = new ListNode(1);
        ListNode n2 = new ListNode(2);
        ListNode n3 = new ListNode(3);
        ListNode n4 = new ListNode(4);
        ListNode n5 = new ListNode(5);
        ListNode n6 = new ListNode(6);
        ListNode n7 = new ListNode(7);

        n1.next = n2;
        n2.next = n3;
        n3.next = n4;

        n5.next = n6;
        n6.next = n7;
        System.out.println(findFirstCommonNode(n1, n5)); // null
    }

    private static void test3() {
        // 公共结点是最后一个结点
        // 1 - 2 - 3 - 4 
        //                7
        //         5 - 6 /
        ListNode n1 = new ListNode(1);
        ListNode n2 = new ListNode(2);
        ListNode n3 = new ListNode(3);
        ListNode n4 = new ListNode(4);
        ListNode n5 = new ListNode(5);
        ListNode n6 = new ListNode(6);
        ListNode n7 = new ListNode(7);

        n1.next = n2;
        n2.next = n3;
        n3.next = n4;
        n4.next = n7;

        n5.next = n6;
        n6.next = n7;
        System.out.println(findFirstCommonNode(n1, n5)); // 7
    }

    private static void test4() {
        // 公共结点是第一个结点
        // 1 - 2 - 3 - 4 - 5
        // 两个链表全然重合
        ListNode n1 = new ListNode(1);
        ListNode n2 = new ListNode(2);
        ListNode n3 = new ListNode(3);
        ListNode n4 = new ListNode(4);
        ListNode n5 = new ListNode(5);
        ListNode n6 = new ListNode(6);
        ListNode n7 = new ListNode(7);

        n1.next = n2;
        n2.next = n3;
        n3.next = n4;
        n4.next = n5;

        System.out.println(findFirstCommonNode(n1, n1)); // 1
    }


}

执行结果

这里写图片描写叙述

原文地址:https://www.cnblogs.com/yxysuanfa/p/7227510.html