题目
法1:写法不简练
public class Solution {public ListNode getIntersectionNode(ListNode headA, ListNode headB) {if (headA == null || headB == null) {return null;}ListNode curA = headA, curB = headB;int aMeetNull = 0;while (aMeetNull < 2) {if (curA == curB) {return curA;}if (curA.next != null) {curA = curA.next;} else {++aMeetNull;curA = headB;}if (curB.next != null) {curB = curB.next;} else {curB = headA;}}return null;}
}
法2:最佳写法
public class Solution {public ListNode getIntersectionNode(ListNode headA, ListNode headB) {ListNode curA = headA, curB = headB;while (curA != curB) {curA = curA != null ? curA.next : headB;curB = curB != null ? curB.next : headA;}return curA;}
}