JZ36-两个链表的第一个公共结点

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

https://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46?tpId=13&tags=&title=&diffculty=0&judgeStatus=0&rp=1&tab=answerKey

class Solution {
    public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
        if (pHead1 == null || pHead2 == null) {
            return null;
        }
        ListNode temp1 = pHead1, temp2 = pHead2;
        while (temp1 != temp2) {
            if (temp1 != null) {
                temp1 = temp1.next;
            } else {
                temp1 = pHead2; //指针归位,画图
            }
            if (temp2 != null) {
                temp2 = temp2.next;
            } else {
                temp2 = pHead1; //指针归位
            }
        }
        return temp1;
    }
}

全部评论

相关推荐

10-30 16:31
重庆大学 Java
代码飞升_不回私信人...:你说你善于学习,大家都会说。你说你是985,985会替你表达一切
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务