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:
begin to intersect at node c1.
Example 1:
Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
Output: Reference of the node with value = 8
Input Explanation: The intersected node's value is 8 (note that this must not be 0 if the two lists intersect). From the head of A, it reads as [4,1,8,4,5]. From the head of B, it reads as [5,0,1,8,4,5]. There are 2 nodes before the intersected node in A; There are 3 nodes before the intersected node in B.
Example 2:
Input: intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
Output: Reference of the node with value = 2
Input Explanation: The intersected node's value is 2 (note that this must not be 0 if the two lists intersect). From the head of A, it reads as [0,9,1,2,4]. From the head of B, it reads as [3,2,4]. There are 3 nodes before the intersected node in A; There are 1 node before the intersected node in B.
Example 3:
Input: intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
Output: null
Input Explanation: From the head of A, it reads as [2,6,4]. From the head of B, it reads as [1,5]. Since the two lists do not intersect, intersectVal must be 0, while skipA and skipB can be arbitrary values.
Explanation: The two lists do not intersect, so return null.
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.
解题思路:
方法一:
先统计两个链表的长度,再遍历较长的链表,使两个链表等长,之后同时遍历两个链表,如果遍历某个结点时相等,则返回结点值,全部结点遍历完成之后如果未找到交叉结点,则返回NULL
/*
执行用时 : 56 ms, 在Intersection of Two Linked Lists的C提交中击败了95.10% 的用户
内存消耗 : 15.2 MB, 在Intersection of Two Linked Lists的C提交中击败了5.61% 的用户
*/
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
if(headA==NULL||headB==NULL)return NULL;
struct ListNode *p=headA,*q=headB;
int nA=0,nB=0;//统计两链表长度。
int i=0;
while(p!=NULL){
nA++;
p=p->next;
}
while(q!=NULL){
nB++;
q=q->next;
}
p=headA;q=headB;
if(nA>nB){
while(i<(nA-nB)){
i++;
p=p->next;
}
}
else if(nB>nA){
while(i<(nB-nA)){
i++;
q=q->next;
}
}
while(p!=NULL&&q!=NULL){
if(p==q)return p;
p=p->next;
q=q->next;
}
return NULL;
}
方法二:
将B链表表头链接到A链表表尾,如果A和B有交叉点则形成环形链表,否则是单链表。此时题目就变为环形链表找第一个进入环形的结点(思想方法见LeetCode 142.环形链表)
/*
执行用时 : 48 ms, 在Intersection of Two Linked Lists的C提交中击败了98.13% 的用户
内存消耗 : 15.3 MB, 在Intersection of Two Linked Lists的C提交中击败了5.61% 的用户
*/
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
if(headA == NULL || headB == NULL){
return NULL;
}
struct ListNode* aListTail = headA;//指向A链表尾
struct ListNode* p = headA->next;//工作指针
while(p != NULL){
aListTail = aListTail->next;
p = p->next;
}
aListTail->next = headB;
//使用"LeetCode 142.环形链表II"的思想和方法
struct ListNode* slow = headA,*fast = headA;
p = headA;
while(fast != NULL && fast->next != NULL){
fast = fast->next->next;
slow = slow->next;
if(slow == fast){
while(p != slow){
p = p->next;
slow = slow->next;
}
aListTail->next = NULL;
return p;
}
}
aListTail->next = NULL;
return NULL;
}
方法三:
此方法可以想象成是把A链表链接到B链表之后,并且B链表连接到A链表之后,此时A与B变成两个长度相等的大链表,同时遍历两个大链表,第一个相同结点就是交叉结点。
/*
执行用时 : 92 ms, 在Intersection of Two Linked Lists的C提交中击败了37.75% 的用户
内存消耗 : 15.1 MB, 在Intersection of Two Linked Lists的C提交中击败了20.56% 的用户
*/
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
if(headA == NULL || headB == NULL){
return NULL;
}
//工作指针
struct ListNode* p = headA;
struct ListNode* q = headB;
while(p != q){
p = p == NULL ? headB : p->next;
q = q == NULL ? headA : q->next;
}
return p;
}
暴力算法:
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
if(!headA || !headB) return NULL;
struct ListNode* pB = headB;
while(headA)
{
pB = headB;
while(pB)
{
if(headA == pB)
{
return headA;
}
pB = pB->next;
}
headA = headA->next;
}
return headA;
}
后记:
LeetCode 142. Linked List Cycle II(环形链表II) -- c语言