Intersection of Two Linked Lists

public class Solution {
		public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
			if (headA == null || headB == null) return null;
			ListNode tempA = headA;
			ListNode tempB = headB;
			int loopTime = 0;
			while (loopTime != 2) {
				if (tempA == null) {
					loopTime++;
					tempA = headB;
				}
				if (tempB == null) {
					tempB = headA;
				}
				if (tempA == tempB) return tempA;
				tempA = tempA.next;
				tempB = tempB.next;
			}
			return null;
		}
}

最后更新于

这有帮助吗?