NowCoder《剑指offer》52--两个链表的第一个公共结点https://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46?tpId=13&tqId=11189&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
题目描述
输入两个链表,找出它们的第一个公共结点。
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
解题思路
关键:如果有公共结点,两个链表从第一个公共结点开始,之后所有的结点都是重合的,不可能再出现分叉。
1 追赶法
4种情况
①长度相同有公共结点,p1等于p2。
②长度相同无公共结点,都等于NULL。
③长度不同有公共结点,p1为空转p2,p2为空转p1,走着走着就同步了,最后p1等于p2。
④长度不同无公共结点,最后都等于NULL。
代码所基于的想法是,将两个链表L1和L2进行拼接,得到L1+L2和L2+L1两个拼接结果。这两个拼接后的链表长度是一致的,那么逐个判断即可。
(1)较短的链走完,测出了距离差
(2)这时,较短链的指针指向较长链的开头,两个指针一起走,后面的指针走完时,前面的指针已经走完距离差了
(3)这时,两个指针已经交换了之前指向的链表,并且消除了距离差,接下来要么为null,要么为公共节点
class Solution {
public:
ListNode* FindFirstCommonNode( ListNode* pHead1, ListNode* pHead2) {
auto p1 = pHead1, p2 = pHead2;
while(p1 != p2) {
p1 = (p1 ? p1->next : pHead2);
p2 = (p2 ? p2->next : pHead1);
}
return p1;
}
};
2 构建哈希表
class Solution {
public:
ListNode* FindFirstCommonNode( ListNode* pHead1, ListNode* pHead2) {
auto current1 = pHead1, current2 = pHead2;
unordered_map<ListNode*, int> hashMap;
while (current1 != NULL) {
hashMap.insert(pair<ListNode*,int>(current1, 1));
current1 = current1->next;
}
while (current2 != NULL) {
if (hashMap.find(current2) != hashMap.end()) return current2;
current2 = current2->next;
}
return NULL;
}
};
3 直接遍历
时间复杂度O(m+n)
class Solution {
public:
ListNode* FindFirstCommonNode( ListNode* pHead1, ListNode* pHead2) {
auto current1 = pHead1, current2 = pHead2;
if (!pHead1 || !pHead2) return nullptr;
int length1 = getLength(current1), length2 = getLength(current2); // 两连表的长度差
if (length1 >= length2) {// 如果链表1的长度大于链表2的长度
int len = length1 - length2;
while (len--) current1 = current1->next;// 先遍历链表1,遍历的长度就是两链表的长度差
} else if (length1 < length2) {// 如果链表2的长度大于链表1的长度
int len = length2 - length1;
while (len--) current2 = current2->next;// 先遍历链表1,遍历的长度就是两链表的长度差
}
while(current1!=current2){//开始齐头并进,直到找到第一个公共结点
current1 = current1->next;
current2 = current2->next;
}
return current1;
}
static int getLength(ListNode* pHead) { // 求指定链表的长度
int length = 0;
auto current = pHead;
while (current) {
length++;
current = current->next;
}
return length;
}
};
leetcode 160 答案
class Solution {
int length(ListNode *head) {
int res = 0;
for(ListNode *p = head; p; p = p->next) ++res;
return res;
}
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
int lenA = length(headA), lenB = length(headB);
for(int i = lenB; i < lenA; ++i) headA = headA->next;
for(int i = lenA; i < lenB; ++i) headB = headB->next;
while (headA != headB) {
headA = headA->next;
headB = headB->next;
}
return headA;
}
};
LeetCode-160. Intersection of Two Linked Lists [C++][Java]_贫道绝缘子的博客-CSDN博客Given the heads of two singly linked-listsheadAandheadB, returnthe node at which the two lists intersect. If the two linked lists have no intersection at all, returnnull.https://blog.csdn.net/qq_15711195/article/details/126190342?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22126190342%22%2C%22source%22%3A%22qq_15711195%22%7D&ctrtid=JUeDp
相关问题
如果只是判断是否存在交点,那么就是另一个问题。有两种解法:
- 把第一个链表的结尾连接到第二个链表的开头,看第二个链表是否存在环;
- 或者直接比较两个链表的最后一个节点是否相同。