# 160. Intersection of Two Linked Lists

## 1.問題

* 返回開始重複的node

![](/files/-LJDVxSpZbqXHz_ZONym)

## 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

![](/files/-LJDWuK7HK_ZmmY3yURD)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jenhsuan.gitbook.io/algorithm/leetcode/160.-intersection-of-two-linked-lists.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
