两个链表的第一个公共结点(链表)

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

public class ListNode {
    int val;
    ListNode next = null;

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

思路一:

找出2个链表的长度,然后让长的先走两个链表的长度差,然后再一起走 (因为2个链表用公共的尾部)。

public class Solution {
    public static void main(String[] args) {
        ListNode p1 = new ListNode(6);
        ListNode p2 = new ListNode(7);
        p1.next = p2;
        ListNode pp1 = new ListNode(1);
        pp1.next = p1;
        Solution s = new Solution();
        System.out.println(s.FindFirstCommonNode(p1,pp1).val);
    }
    public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
        if (pHead1 == null || pHead2 == null) return null;
        int len1 = ListLen(pHead1);
        int len2 = ListLen(pHead2);
        if (len1 >= len2)
            pHead1 = walkStep(pHead1, len1 - len2);
        else if (len1 < len2)
            pHead2 = walkStep(pHead2, len2 - len1);
        while (pHead1 != null)
        {
            if (pHead1 == pHead2) return pHead1;
            else {
                pHead1 = pHead1.next;
                pHead2 = pHead2.next;
            }
        }
        return null;
    }
    private int ListLen(ListNode pHead)
    {
        int len = 0;
        ListNode current = pHead;
        while (current != null)
        {
            len++;
            current = current.next;
        }
        return len;
    }
    private ListNode walkStep(ListNode pHead, int step)
    {
        while (step > 0)
        {
            pHead = pHead.next;
            step--;
        }
        return pHead;
    }
}

思路二:

假定:List1长度: a+n,List2长度:b+n, 且 a<b         

那么,p1会先到链表尾部,这时p2走到a+n位置,将p1换成List2头部。         

接着,p2 再走b+n-(n+a) =b-a步到链表尾部,这时p1也走到List2的b-a位置,还差a步就到可能的第一个公共节点。         

将p2换成 List1头部,p2走a步也到可能的第一个公共节点。

如果恰好p1==p2,那么p1就是第一个公共节点。 

或者p1和p2一起走n步到达列表尾部,二者没有公共节点,退出循环。 

同理a>=b。

时间复杂度O(n+a+b)

public class Solution {
    public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
        if (pHead1 == null || pHead2 == null) return null;
        ListNode p1 = pHead1;
        ListNode p2 = pHead2;
        while (p1 != p2)
        {
            p1 = (p1 == null)? pHead2 : p1.next;
            p2 = (p2 == null) ? pHead1 : p2.next;
        }
        return p1;
    }
}


思路三:利用HashMap

import java.util.HashMap;
import java.util.Map;
public class Solution {
    public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
        if (pHead1 == null || pHead2 == null) return null;
        Map<ListNode, Integer> map = new HashMap<>();
        while (pHead1 != null)
        {
            map.put(pHead1, null);
            pHead1 = pHead1.next;
        }
        while (pHead2 != null)
        {
            if (map.containsKey(pHead2)) return pHead2;
            else pHead2 = pHead2.next;
        }
        return pHead2;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值