160. 相交链表

160. 相交链表_第1张图片

/**
 * 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) {
              set nodeset;
              while(headA)
              {
                  nodeset.insert(headA);
                  headA=headA->next;
              }
              while(headB)
              {
                  if(nodeset.find(headB)!=nodeset.end())
                  {
                      return headB;
                  }
                  headB=headB->next;
              }
        return NULL;
    }
};
160. 相交链表_第2张图片 160. 相交链表_第3张图片

你可能感兴趣的:(LeetCode)