(剑指offer)两个链表的第一个公共节点

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ccnuacmhdu/article/details/84728386

时间限制:1秒 空间限制:32768K 热度指数:153107
本题知识点: 链表

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

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

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
        ListNode common = null;
        if(pHead1==null || pHead2==null){
            return common;
        }
        while(pHead1!= null){
            ListNode tmp = pHead2;
            while(tmp!=null){
                if(tmp.val == apHead1.val){
                    return tmp;
                }
                tmp = tmp.next;
            }
            pHead1 = pHead1.next;
        }
        return common;
    }
}

猜你喜欢

转载自blog.csdn.net/ccnuacmhdu/article/details/84728386