厚积薄发打卡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
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值