题目
解题思路
Code
Java
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if (headA == null || headB == null)
return null;
ListNode a = headA;
ListNode b = headB;
while (a != b ) {
a = a != null ? a.next : headB;
b = b != null ? b.next : headA;
}
return a;
}
Go
func getIntersectionNode(headA, headB *ListNode) *ListNode {
a , b := headA, headB;
for a != b {
if (a != nil) {
a = a.Next
} else {
a = headB
}
if (b != nil) {
b = b.Next
} else {
b = headA;
}
}
return a
}