【题目】:
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.
【思路】
其实也是看了别人的想法,才试着实现了。
两个list,找共同的部分的开头,问题是,不知道长度,记录不了位置,所以不知道走到哪里才能比较
怎么样才能让两个list的指针同步呢?
答案就是,两个list “相加”,两个指针分别从l1 , l2开始 走到l2 ,l1 ,这样两个指针每次都增加一个,然后走的总共长度相同,而且最后几位,如果有相同的,一定是相等的!
L1 : 1 ->2 ->3 ->4-X->X->X
L2 : 5->6-> X ->X->X
L1 + L2 : 1 ->2 ->3 ->4->X->X->X ->5->6-> X ->X->X
L2 +L1 : 5->6-> X ->X->X->1 ->2 ->3 ->4->X->X->X
但是实现起来还是有许多细节需要考虑:
比如p1, p2 走到l1,l2终点的时候怎么衔接,另外只能衔接一次,不能无限循环,所以要设定一个flag,如果衔接过一次,就不能再次衔接了。
而且因为l1 ,l2的长度不相等,指不定p1,p2谁先衔接,如果p1先到衔接点,要在判断一下p2的状况!!如果也要衔接了,就衔接, 如果不要的话,正常加一,然后continue!!到下一循环,因为两个点都已经更新了!!对于p2,vice versu, 所以写了两个大判断,每一个里面包含对另一个的判断。感觉应该会有更优化的结果。
如果都不是衔接点,那就是正常+1.
如果相等了,那么就return p1, p2随便哪个点。
如果循环都结束了还没有找到,那么就是return null
【我提交并且通过的代码】
Runtime: 337 ms
java里面中等水平吧,哈哈,还不错~
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode l1 = headA;
ListNode p1 = headA;
ListNode l2 = headB;
ListNode p2 = headB;
boolean flag1 = true;
boolean flag2 = true;
while(p1!=null && p2 != null){
if (p1 == p2) return p2;
else{
if(p1.next==null && flag1 == true) {
p1 = l2;
flag1 = false;
if(p2.next == null && flag2 == true) {
p2 = l1;
flag2 = false;
continue;
}
p2 = p2.next;
continue;
}
if(p2.next==null && flag2 ==true) {
p2 = l1;
flag2 = false;
if(p1.next==null && flag1 == true) {
p1 = l2;
flag1 =false;
continue;
}
p1 = p1.next;
continue;
}
p1 = p1.next;
p2 = p2.next;
}
}
return null;
}
}
There are many solutions to this problem:
- Brute-force solution (O(mn) running time, O(1) memory):
For each node ai in list A, traverse the entire list B and check if any node in list B coincides with ai.
- Hashset solution (O(n+m) running time, O(n) or O(m) memory):
Traverse list A and store the address / reference to each node in a hash set. Then check every node bi in list B: if bi appears in the hash set, then bi is the intersection node.
- Two pointer solution (O(n+m) running time, O(1) memory):
- Maintain two pointers pA and pB initialized at the head of A and B, respectively. Then let them both traverse through the lists, one node at a time.
- When pA reaches the end of a list, then redirect it to the head of B (yes, B, that's right.); similarly when pB reaches the end of a list, redirect it the head of A.
- If at any point pA meets pB, then pA/pB is the intersection node.
- To see why the above trick would work, consider the following two lists: A = {1,3,5,7,9,11} and B = {2,4,9,11}, which are intersected at node '9'. Since B.length (=4) < A.length (=6), pB would reach the end of the merged list first, because pB traverses exactly 2 nodes less than pA does. By redirecting pB to head A, and pA to head B, we now ask pB to travel exactly 2 more nodes than pA would. So in the second iteration, they are guaranteed to reach the intersection node at the same time.
- If two lists have intersection, then their last nodes must be the same one. So when pA/pB reaches the end of a list, record the last element of A/B respectively. If the two last elements are not the same one, then the two lists have no intersections.
看到的更优的解法:
思路是一致的,但是分开单独处理的p1,p2,貌似见过,不错!这次一定要记住!
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode A = headA;
ListNode B = headB;
int times = 0;
while(times<=1){
if(A==B)
return A;
if(A==null){
A=headB;
times++;
}
else
A=A.next;
if(B==null)
B=headA;
else
B=B.next;
}
return null;
}
Runtime: 352 ms
用hashset 的做法:
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
HashSet<ListNode> set = new HashSet<ListNode>();
while(headA!=null||headB!=null){
if(headA!=null) {
if(set.contains(headA)){
return headA;
} else {
set.add(headA);
}
headA=headA.next;
}
if(headB!=null) {
if(set.contains(headB)) {
return headB;
} else {
set.add(headB);
}
headB=headB.next;
}
}
return null;
}
}