380 · Intersection of Two Linked Lists
Algorithms
Medium
Accepted Rate
46%
Description
Solution
Notes
Discuss
Leaderboard
Record
Description
Write a program to find the node at which the intersection of two singly linked lists begins.
Wechat reply 【Google】 get the latest requent Interview questions. (wechat id : jiuzhang0607)
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.
Example
Example 1:
Input:
A: a1 → a2
↘
c1 → c2 → c3
↗
B: b1 → b2 → b3
Output: c1
Explanation :begin to intersect at node c1.
Example 2:
Input:
Intersected at 6
1->2->3->4->5->6->7->8->9->10->11->12->13->null
6->7->8->9->10->11->12->13->null
Output: Intersected at 6
Explanation:begin to intersect at node 6.
Challenge
Your code should preferably run in O(n) time and use only O(1) memory.
解法1:
链表A走完后从链表B头开始走,链表B走完后从链表A头开始走,
假设链表A和链表B公用部分长度为c,相遇前A长度为a,B长度为b,则两个指针都走完a+b+c后会在汇合点相遇。
/**
* Definition of singly-linked-list:
* class ListNode {
* public:
* int val;
* ListNode *next;
* ListNode(int val) {
* this->val = val;
* this->next = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param headA: the first list
* @param headB: the second list
* @return: a ListNode
*/
ListNode * getIntersectionNode(ListNode * headA, ListNode * headB) {
if (!headA || !headB) return NULL;
ListNode *pA = headA;
ListNode *pB = headB;
while(pA && pB) {
if (pA == pB) return pA;
pA = pA->next;
pB = pB->next;
if (!pA) pA = headB;
if (!pB) pB = headA;
}
return NULL;
}
};
可以简化为:
class Solution {
public:
/**
* @param headA: the first list
* @param headB: the second list
* @return: a ListNode
*/
ListNode * getIntersectionNode(ListNode * headA, ListNode * headB) {
// write your code here
if (headA == nullptr || headB == nullptr) {
return nullptr;
}
ListNode *pA = headA, *pB = headB;
while (pA != pB) {
pA = (pA == nullptr) ? headB : pA->next;
pB = (pB == nullptr) ? headA : pB->next;
}
return pA;
}
};
这个简化的代码更好。因为如果链表A和B不相遇的话,那么pA和pB都走完了A链表+B链表的长度后会都变成nullptr,此时会返回nullptr。
解法2:经典的解法。先测出两个链表的长度,长度差为gap,然后长链表从头开始走gap步,此时两个链表开始同步走,直至相遇。若不相遇,走到终点返回nullptr。
/**
* Definition of singly-linked-list:
* class ListNode {
* public:
* int val;
* ListNode *next;
* ListNode(int val) {
* this->val = val;
* this->next = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param headA: the first list
* @param headB: the second list
* @return: a ListNode
*/
ListNode * getIntersectionNode(ListNode * headA, ListNode * headB) {
// write your code here
if (headA == nullptr || headB == nullptr) {
return nullptr;
}
int lenA = 0, lenB = 0;
ListNode *pA = headA, *pB = headB;
while(pA) {
lenA++;
pA = pA->next;
}
while(pB) {
lenB++;
pB = pB->next;
}
ListNode *pLong = lenA >= lenB ? headA : headB;
ListNode *pShort = lenA >= lenB ? headB : headA;
int lenLong = max(lenA, lenB);
int lenShort = min(lenA, lenB);
int gap = lenLong - lenShort;
for (int i = 0; i < gap; i++) pLong = pLong->next;
while (pLong != pShort) {
pLong = pLong->next;
pShort = pShort->next;
}
return pLong;
}
};