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

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

http://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 iter1 = pHead1;
        ListNode iter2 = pHead2;
        while(iter1 != iter2){
            iter1 = (iter1 == null) ? pHead2 : iter1.next;
            iter2 = (iter2 == null) ? pHead1 : iter2.next;
        }
        return iter1;
    }
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
昨天 11:45
你不要过来啊啊啊啊啊啊啊
码农索隆:对面:“今天你不面也得面”
点赞 评论 收藏
分享
06-24 19:27
云南大学 Java
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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