厚积薄发打卡Day105:链表(三)两个链表的第1个重合节点

厚积薄发打卡Day105:链表(三)两个链表的第1个重合节点,第1张

厚积薄发打卡Day105:链表(三)两个链表的第1个重合节点 厚积薄发打卡Day105:链表(三)<两个链表的第1个重合节点> 两个链表的第1个重合节点

输入两个单向链表,请问如何找出它们的第1个重合节点。例如,图中的两个链表的第1个重合节点的值是4。

思路
  1. 整体思路,从两个链表的相对同一位置出发遍历,得到首个相同的节点即为重合节点
  2. 上图输入了:
    • 链表1:1->2->3->4->5->6
    • 链表2:7->8->4->5->6
  3. 因为存在输入的两个链表长度不一致的情况,所以需要一定的校验:
    1. 将指针设置为两个链表的相对相同的位置:
    2. 链表1从【2节点】开始遍历,链表2从【7节点】开始遍历
实现
  • 手动校准:

    public class FindFirstCommonNode02 {
    
        public static ListNode findFirstCommonNode(ListNode pHead1, ListNode pHead2) {
            //分别求出两个链表的长度
            int count1 = countNodeList(pHead1);
            int count2 = countNodeList(pHead2);
            //求绝对差值
            int delta = Math.abs(count1 - count2);
            //判断长、短链表
            ListNode longer = count1 > count2 ? pHead1 : pHead2;
            ListNode shorter = count1 > count2 ? pHead2 : pHead1;
            //重置指针
            ListNode node1 = longer;
            for (int i = 0; i < delta; i++) {
                node1 = node1.next;
            }
            //同步遍历
            ListNode node2 = shorter;
            while (node1 != node2) {
                node2 = node2.next;
                node1 = node1.next;
            }
            //相同就返回
            return node1;
        }
    
        private static int countNodeList(ListNode head) {
            int count = 0;
            while (head != null) {
                count++;
                head = head.next;
            }
            return count;
        }
    
        public static void main(String[] args) {
            ListNode node1 = new ListNode(1);
            ListNode node2 = new ListNode(2);
            ListNode node3 = new ListNode(3);
            ListNode node4 = new ListNode(4);
            ListNode node5 = new ListNode(5);
            ListNode node6 = new ListNode(6);
            node1.next = node2;
            node2.next = node3;
            node3.next = node4;
            node4.next = node5;
            node5.next = node6;
            ListNode node7 = new ListNode(7);
            ListNode node8 = new ListNode(8);
            node7.next = node8;
            node8.next = node4;
            node4.next = node5;
            node5.next = node6;
            ListNode.printNodeList(node1);
            ListNode.printNodeList(node7);
    
            ListNode commonNode = findFirstCommonNode(node1, node7);
            System.out.println(commonNode);
        }
    }
    
    1->2->3->4->5->6->NULL
    7->8->4->5->6->NULL
    4
    
  • 循环校准:

    循环重置,将两个链表的指针互换,得到差值,具体详见:剑指offer打卡day06:两个链表的第一个公共节点

    public class FindFirstCommonNode01 {
    
        public static ListNode findFirstCommonNode(ListNode pHead1, ListNode pHead2) {
            if (pHead1 == null || pHead2 == null) {
                return null;
            }
            ListNode p1 = pHead1;
            ListNode p2 = pHead2;
    
            while (p1 != p2) {
                p1 = p1.next;
                p2 = p2.next;
                if (p1 != p2) {
    
                    if (p1 == null) {
                        p1 = pHead2;
                    }
                    if (p2 == null) {
                        p2 = pHead1;
                    }
                }
            }
            return p1;
        }
    
        public static void main(String[] args) {
            ListNode node1 = new ListNode(1);
            ListNode node2 = new ListNode(2);
            ListNode node3 = new ListNode(3);
            ListNode node4 = new ListNode(4);
            ListNode node5 = new ListNode(5);
            ListNode node6 = new ListNode(6);
            node1.next = node2;
            node2.next = node3;
            node3.next = node4;
            node4.next = node5;
            node5.next = node6;
            ListNode node7 = new ListNode(7);
            ListNode node8 = new ListNode(8);
            node7.next = node8;
            node8.next = node4;
            node4.next = node5;
            node5.next = node6;
            ListNode.printNodeList(node1);
            ListNode.printNodeList(node7);
    
            ListNode commonNode = findFirstCommonNode(node1, node7);
            System.out.println(commonNode);
        }
    }
    
    1->2->3->4->5->6->NULL
    7->8->4->5->6->NULL
    4
    

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5692841.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存