# 160. Intersection of Two Linked Lists

## 1.問題

* 返回開始重複的node

![](https://901207480-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LGKoChvN9am4__HCIRK%2F-LJDQENQbAMEvjEKJe_m%2F-LJDVxSpZbqXHz_ZONym%2F2018080305.jpg?alt=media\&token=db525276-5997-4db6-a473-48ef3e766ad7)

## 2.想法

* 用map紀錄s出現過的node, 再看看t是否有出現相同的node

## 3.程式碼

```
/**
 * 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) {
        unordered_map<ListNode*, int> m;
        ListNode *head = headA;
        while (head)
        {
            m[head] = 1;
            head = head->next;
        }
        
         head = headB;
         while (head)
         {
            if (m.find(head) != m.end())
            {
                return head;
            }
            head = head->next;
         }
        return NULL;
        
    }
};
```

## 4.Performance

![](https://901207480-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LGKoChvN9am4__HCIRK%2F-LJDQENQbAMEvjEKJe_m%2F-LJDWuK7HK_ZmmY3yURD%2F2018080306.jpg?alt=media\&token=b1ea34b5-5e7c-492d-bc3a-eae4a92db844)
