题解 | #两个链表的第一个公共结点#
两个链表的第一个公共结点
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;
}
}
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;
}
}