题意:找到两条链表相交的结点。
思路:暴力查找。
/**
* 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) {
if(headA == NULL || headB == NULL) return NULL;
map<ListNode*, bool> mym;
ListNode *temp;
temp = headA;
std::map<ListNode*, bool>::iterator it;
while(temp) {
mym[temp] = true;
temp = temp->next;
}
temp = headB;
while(temp) {
it = mym.find(temp);
if(it != mym.end()) return temp;
temp = temp->next;
}
return NULL;
}
};

本文介绍了一种通过暴力查找的方法来解决两个链表相交节点的问题。使用C++实现了一个解决方案,利用哈希表记录第一个链表的所有节点,然后遍历第二个链表找到交点。
1065

被折叠的 条评论
为什么被折叠?



