https://leetcode-cn.com/problems/intersection-of-two-linked-lists/
题目
编写一个程序,找到两个单链表相交的起始节点。
如下面的两个链表:
在节点 c1 开始相交。
示例1
输入:intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
输出:Reference of the node with value = 8
输入解释:相交节点的值为 8 (注意,如果两个链表相交则不能为 0)。从各自的表头开始算起,链表 A 为 [4,1,8,4,5],链表 B 为 [5,0,1,8,4,5]。在 A 中,相交节点前有 2 个节点;在 B 中,相交节点前有 3 个节点。
示例2
输入:intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
输出:Reference of the node with value = 2
输入解释:相交节点的值为 2 (注意,如果两个链表相交则不能为 0)。从各自的表头开始算起,链表 A 为 [0,9,1,2,4],链表 B 为 [3,2,4]。在 A 中,相交节点前有 3 个节点;在 B 中,相交节点前有 1 个节点。
示例3
输入:intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
输出:null
输入解释:从各自的表头开始算起,链表 A 为 [2,6,4],链表 B 为 [1,5]。由于这两个链表不相交,所以 intersectVal 必须为 0,而 skipA 和 skipB 可以是任意值。
解释:这两个链表不相交,因此返回 null。
注意:
- 如果两个链表没有交点,返回 null.
- 在返回结果后,两个链表仍须保持原有的结构。
- 可假定整个链表结构中没有循环。
- 程序尽量满足 O(n) 时间复杂度,且仅用 O(1) 内存。
思路1
可以通过set实现。set的特性是无法插入重复元素。即插入时,如果有重复元素,会在插入结果中返回一个false。
pair<set<ListNode*>::iterator, bool> ret = s.insert(headB);
利用这一特性,可以寻找交集。但要注意,插入set的应该是链表每个节点的地址,而不是val,因为val有重复,我们寻找的是地址的相交点。
答案1
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) {
/***************** 填写 *******************/
set<ListNode*> s;
while (headA) //将A全部放入set
{
s.insert(headA); //将地址插入set 而不是值域(值域难免有重复)
headA = headA->next;
}
pair<set<ListNode*>::iterator, bool> ret;
while (headB) //尝试插入headB的元素,第一次相撞即为交点
{
ret = s.insert(headB);
if (ret.second) //ret.second表示取到第二个数据
headB = headB->next; //表示插入成功
else
return headB; //插入失败 找到交点
}
return NULL;
/***************** 结束 *******************/
}
};
完整代码1
#include <iostream>
#include <set>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) {
/***************** 填写 *******************/
set<ListNode*> s;
while (headA) //将A全部放入set
{
s.insert(headA); //将地址插入set 而不是值域(值域难免有重复)
headA = headA->next;
}
pair<set<ListNode*>::iterator, bool> ret;
while (headB) //尝试插入headB的元素,第一次相撞即为交点
{
ret = s.insert(headB);
if (ret.second) //ret.second表示取到第二个数据
headB = headB->next; //表示插入成功
else
return headB; //插入失败 找到交点
}
return NULL;
/***************** 结束 *******************/
}
};
void showList(ListNode* head) //遍历
{
while (head)
{
cout << head->val << "\t";
head = head->next;
}
cout << endl;
}
void test01()
{
/*8
[4,1, 8,4,5]
[5,6,1, 8,4,5]
*/
ListNode a1(4); //创建数据用于测试
ListNode a2(1);
ListNode c1(8);
ListNode c2(4);
ListNode c3(5);
ListNode b1(5);
ListNode b2(6);
ListNode b3(1);
a1.next = &a2;
a2.next = &c1;
c1.next = &c2;
c2.next = &c3;
b1.next = &b2;
b2.next = &b3;
b3.next = &c1;
c3.next = NULL;
ListNode* headA = &a1; //头指针
ListNode* headB = &b1;
cout << "Before:" << endl;
showList(headA); //遍历
showList(headB);
cout << "After: " << endl;
Solution s;
ListNode* result = s.getIntersectionNode(headA, headB); //寻找交点
showList(result); //遍历
}
int main()
{
test01();
system("pause");
return 0;
}
复杂度分析1
set的时间复杂度是O(nlogn),所以整体时间复杂度O(nlogn),空间复杂度O(n)。
思路2
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
int len_a=0; //计数坐标 0开始
ListNode *t_a=headA;
while(t_a){
len_a=len_a+1;
t_a=t_a->next;
}
int len_b=0; //计数坐标 0开始
ListNode *t_b=headB;
while(t_b){
len_b=len_b+1;
t_b=t_b->next;
}
//从差值寻找交点
if(len_a>len_b){
int cha=len_a-len_b;
for(int i=0;i<cha;i++)
headA=headA->next; //对齐上下指针
while(headA&&headB){
if(headA==headB)
return headA;
headA=headA->next;
headB=headB->next;
}
}
else{
int cha=len_b-len_a;
for(int i=0;i<cha;i++)
headB=headB->next; //对齐上下指针
while(headA&&headB){
if(headA==headB)
return headA;
headA=headA->next;
headB=headB->next;
}
}
return NULL;
}
};
完整代码2
#include <iostream>
#include <set>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) {
/***************** 填写 *******************/
int lenA = 0, lenB = 0;
ListNode* tA = headA;
ListNode* tB = headB;
while (tA) //1.计算长度
{
++lenA;
tA = tA->next;
}
while (tB) //1.计算长度
{
++lenB;
tB = tB->next;
}
if (lenB > lenA) //如果B比较长
{
for (int i = 0; i < lenB - lenA; i++) //2.对齐指针
headB = headB->next;
while (headA)
{
if (headA == headB) //如果节点地址相同
return headA;
headA = headA->next;
headB = headB->next;
}
}
else
{
for (int i = 0; i < lenA - lenB; i++) //2.对齐指针
headA = headA->next;
while (headA)
{
if (headA == headB) //如果节点地址相同
return headA;
headA = headA->next;
headB = headB->next;
}
}
return NULL;
/***************** 结束 *******************/
}
};
void showList(ListNode* head) //遍历
{
while (head)
{
cout << head->val << "\t";
head = head->next;
}
cout << endl;
}
void test01()
{
/*8
[4,1, 8,4,5]
[5,6,1, 8,4,5]
*/
ListNode a1(4); //创建数据用于测试
ListNode a2(1);
ListNode c1(8);
ListNode c2(4);
ListNode c3(5);
ListNode b1(5);
ListNode b2(6);
ListNode b3(1);
a1.next = &a2;
a2.next = &c1;
c1.next = &c2;
c2.next = &c3;
b1.next = &b2;
b2.next = &b3;
b3.next = &c1;
c3.next = NULL;
ListNode* headA = &a1; //头指针
ListNode* headB = &b1;
cout << "Before:" << endl;
showList(headA); //遍历
showList(headB);
cout << "After: " << endl;
Solution s;
ListNode* result = s.getIntersectionNode(headA, headB); //寻找交点
showList(result); //遍历
}
int main()
{
test01();
system("pause");
return 0;
}
复杂度分析2
空间O(1),时间O(n)