题目描述
输入两个链表,找出它们的第一个公共结点。
解题思路
只会暴力到怀疑人生,无果。书上给出了两种思路。由于两个链表尾部是一样的,所以方法1是用两个辅助栈存放两个链表,然后最后一个值和next地址相同的结点就是第一个公共结点,时空复杂度都是O(m+n)。方法2就更妙了,先获得两个链表的长度,然后让长的先走长-短的差的步数,之后第一个值和next都相同的结点就是公共结点,时间复杂度是O(m+n)。
Code
- 方法2
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
int GetLengthOfList(ListNode* pHead) {
if(!pHead) return 0;
return 1+GetLengthOfList(pHead->next);
}
ListNode* GetKStepList(ListNode* pHead, int kStep) {
if(!pHead) return nullptr;
if(kStep > 0) {
return GetKStepList(pHead->next, kStep-1);
}
return pHead;
}
ListNode* FindFirstCommonNode(ListNode* pHead1, ListNode* pHead2) {
int length1 = GetLengthOfList(pHead1), length2 = GetLengthOfList(pHead2);
ListNode* pNode1 = pHead1, *pNode2 = pHead2;
int difference = length1-length2;
if(difference > 0) {
pNode1 = GetKStepList(pHead1, difference);
} else {
pNode2 = GetKStepList(pHead2, -difference);
}
while(pNode1 && pNode2 && (pNode1->val != pNode2->val || pNode1->next != pNode2->next)) {
pNode1 = pNode1->next;
pNode2 = pNode2->next;
}
if(pNode1 && pNode2 && pNode1->val == pNode2->val && pNode1->next == pNode2->next) {
return pNode1;
}
return nullptr;
}
};
- java
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
import java.util.LinkedList;
public class Solution {
public LinkedList<ListNode> GetLinkedList(ListNode pHead) {
LinkedList<ListNode> list = new LinkedList<ListNode>();
while(pHead != null) {
list.add(pHead);
pHead = pHead.next;
}
return list;
}
public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
ListNode result = null;
LinkedList<ListNode> list1 = GetLinkedList(pHead1);
LinkedList<ListNode> list2 = GetLinkedList(pHead2);
while(!list1.isEmpty() && !list2.isEmpty()) {
ListNode temp1 = list1.removeLast();
ListNode temp2 = list2.removeLast();
if(temp1 != temp2) {
break;
}
result = temp1;
}
return result;
}
}
- 看完评论区被一个代码惊艳到了,只要遍历两个链表和的长度就可以找到第一个公共结点
链接:https://www.nowcoder.com/questionTerminal/6ab1d9a29e88450685099d45c9e31e46
来源:牛客网
class Solution {
public:
ListNode* FindFirstCommonNode( ListNode *pHead1, ListNode *pHead2) {
ListNode *p1 = pHead1;
ListNode *p2 = pHead2;
while(p1!=p2){
p1 = (p1==NULL ? pHead2 : p1->next);
p2 = (p2==NULL ? pHead1 : p2->next);
}
return p1;
}
};
总结
看评论区代码,公共结点的比较可以直接用指针判等比较。。