题解 | #两个链表的第一个公共结点#

两个链表的第一个公共结点

http://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46

利用哈希表set()判断节点是否出现过

struct ListNode {
	int val;
	struct ListNode *next;
	ListNode(int x) :
			val(x), next(NULL) {
	}
};*/
class Solution {
public:
    ListNode* FindFirstCommonNode( ListNode* pHead1, ListNode* pHead2) {
        unordered_set<ListNode*> pH;
        ListNode *cur = pHead1;
        while(cur){
            pH.insert(cur);
            cur = cur->next;
        }
        cur = pHead2;
        while(cur){
            if(pH.find(cur) != pH.end()){
                return cur;
            }
            cur = cur->next;
        }
        
        return nullptr;
        
    }
};
全部评论

相关推荐

肥沃富饶:可能初创公司,老板不懂技术
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务