题解 | #两个链表的第一个公共结点#

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

https://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46

/*
public class ListNode
{
    public int val;
    public ListNode next;
    public ListNode (int x)
    {
        val = x;
    }
}*/
class Solution {
    public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
        // write code here
        ListNode t1 = pHead1;
        ListNode t2 = pHead2;
        while (t2 != null) {
            t1 = pHead1;
            while (t1 != null) {
                if (t1 == t2) {
                    return t1;
                } else {
                    t1 = t1.next;
                }
            }
            t2 = t2.next;
        }
        return null;
    }
}

全部评论

相关推荐

06-26 15:33
青岛工学院 Java
积极的秋田犬要冲国企:他现在邀请我明天面试
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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