 # [LeetCode] 160. Intersection of Two Linked Lists 求两个链表的交集

Write a program to find the node at which the intersection of two singly linked lists begins.

For example, the following two linked lists:

A:          a1 → a2
↘
c1 → c2 → c3
↗
B:     b1 → b2 → b3


begin to intersect at node c1.

Notes:

• If the two linked lists have no intersection at all, return null.
• The linked lists must retain their original structure after the function returns.
• You may assume there are no cycles anywhere in the entire linked structure.
• Your code should preferably run in O(n) time and use only O(1) memory.

Credits:
Special thanks to @stellari for adding this problem and creating all test cases.

Java： Solution 1

public class Solution {
if (lenA > lenB) {
for (int i = 0; i < lenA - lenB; ++i) headA = headA.next;
} else {
for (int i = 0; i < lenB - lenA; ++i) headB = headB.next;
}
}
}
int cnt = 0;
++cnt;
}
return cnt;
}
}


Java: Solution 2

public class Solution {
while (a != b) {
a = (a != null) ? a.next : headB;
b = (b != null) ? b.next : headA;
}
return a;
}
}　

Python:

class Solution:
# @param two ListNodes
# @return the intersected ListNode
return None

pa = headA # 2 pointers

while pa is not pb:
# if either pointer hits the end, switch head and continue the second traversal,
# if not hit the end, just move on to next
pa = headB if pa is None else pa.next
pb = headA if pb is None else pb.next

return pa # only 2 ways to get out of the loop, they meet or the both hit the end=None

# the idea is if you switch head, the possible difference between length would be countered.
# On the second traversal, they either hit or miss.
# if they meet, pa or pb would be the node we are looking for,
# if they didn't meet, they will hit the end at the same iteration, pa == pb == None, return either one of them is the same,None


Python: wo

class Solution(object):
return None

while a != b:
a = a.next if a else headB
b = b.next if b else headA

return a 　

Python: Solution 1

class Solution(object):
if lenA > lenB:
for i in range(lenA - lenB):
elif lenA < lenB:
for i in range(lenB - lenA):

length = 0
length += 1
return length　

Python: Solution 2

class ListNode:
def __init__(self, x):
self.val = x
self.next = None

class Solution:
# @param two ListNodes
# @return the intersected ListNode
begin, tailA, tailB = None, None, None

# a->c->b->c
# b->c->a->c
while curA and curB:
if curA == curB:
begin = curA
break

if curA.next:
curA = curA.next
elif tailA is None:
tailA = curA
else:
break

if curB.next:
curB = curB.next
elif tailB is None:
tailB = curB
else:
break

return begin　　

C++：

class Solution {
public:
if (lenA < lenB) {
for (int i = 0; i < lenB - lenA; ++i) headB = headB->next;
} else {
for (int i = 0; i < lenA - lenB; ++i) headA = headA->next;
}
}
}
int cnt = 0;
++cnt;
}
return cnt;
}
};


C++:

class Solution {
public:
while (a != b) {
a = a ? a->next : headB;
b = b ? b->next : headA;
}
return a;
}
};


[LeetCode] 349. Intersection of Two Arrays 两个数组相交

[LeetCode] 350. Intersection of Two Arrays II 两个数组相交II

# All LeetCode Questions List 题目汇总

posted @ 2018-03-20 06:24  轻风舞动  阅读(347)  评论(0编辑  收藏  举报