首页 > 其他 > 详细

160-相交链表

时间:2020-03-04 14:12:06      阅读:63      评论:0      收藏:0      [点我收藏+]

一、暴力解法

二、哈希表思路

将A链表放入哈希表中,对B链表进行遍历,查询是否有元素存在哈希表中。

哈希表建立方法: set<ListNode*>hash;

技术分享图片
 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
12         set<ListNode*>hash;
13         while(headA)
14         {
15             hash.insert(headA);
16             headA=headA->next;
17         }
18         while(headB)
19         {
20             if(hash.find(headB)!=hash.end())
21             {
22                 return headB;
23             }
24             else
25             {
26                 headB=headB->next;
27             }
28         }
29         return NULL;
30 
31 
32         
33     }
34 };
View Code

 

知识点链接:https://blog.csdn.net/lady_killer9/article/details/79259378

160-相交链表

原文:https://www.cnblogs.com/nxnslc-blog/p/12408710.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!