[LeetCode] Intersection of Two Linked Lists

Write a program to find the node at which the intersection of two singly linked lists begins.


For example, the following two linked lists:

A:          a1 → a2
                   ↘
                     c1 → c2 → c3
                   ↗            
B:     b1 → b2 → b3

begin to intersect at node c1.


Notes:

  • If the two linked lists have no intersection at all, return null.
  • The linked lists must retain their original structure after the function returns.
  • You may assume there are no cycles anywhere in the entire linked structure.
  • Your code should preferably run in O(n) time and use only O(1) memory.

要說在LeetCode上刷題最讓我受益匪淺的地方,大概就是鏈表相關的問題了吧。以前對於鏈表的認識很淺,基本只夠應付“說說鏈表和數組的優缺點”這種問題,真正應用到的機會很少。來了LeetCode才發現,鏈表還可以這麼玩,而且玩的這麼溜。

針對這道題,我想了很久都想不到O(n)時間複雜度的做法(我是真的菜),於是就擼了一個暴力枚舉的做法:
/**
 * 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) {
        ListNode* tempA;
        ListNode* tempB;
        while(headA)
        {
            tempA=headA;
            tempB=headB;
            while(tempB)
            {
                if(tempA==tempB)return tempA;
                tempB=tempB->next;
            }
            headA=headA->next;
        }
        return NULL;
    }
};
畢竟跟指針相關的代碼很容易出錯,爲了確認這樣寫的邏輯有沒有問題,我就試探性地交了一發,居然直接AC了...這要是在ACM,怎麼也是TLE吧。
感覺打ACM的人是不是真的不應該做LeetCode,做多了AC多了,你真的就沒有對罰時的恐懼了,對時間複雜度也視而不見了...

當然這道題還沒有結束,上面的代碼明顯不是最優的。想了一下其實用哈希基本就可以降複雜度了,於是乎再來一發:
/**
 * 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_set<ListNode*> s;
        while(headA)
        {
            s.insert(headA);
            headA=headA->next;
        }
        while(headB)
        {
            if(s.find(headB)!=s.end())return headB;
            headB=headB->next;
        }
        return NULL;
    }
};
哈哈哈過了,用時也還算低,但是直覺告訴我這不是出題人的本意,肯定還有更巧妙的做法。果不其然,圍觀了一下大佬的做法,簡直變身小迷弟給大佬瘋狂打call。這是大佬的代碼:
/**
 * 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) {
        if (!headA||!headB) return NULL;
        ListNode *a=headA,*b=headB;
        while(a!=b)
        {
            a=a?a->next:headB;
            b=b?b->next:headA;
        }
        return a;
    }
};
兩個字:優美
題目保證不出現環,然後大佬就把兩個鏈首尾相連構造成環。有兩個理解上的要點:
  • 假設A長度m,B長度n,兩個指針都會在m+n次循環後同時抵達空指針,使得a!=b不成立退出循環,防止了死循環的出現,返回NULL。
  • 假設倒數第k個點是交集,由於交點之後的點是共用的,所以兩個指針都會在m+n-k次循環後抵達相同節點,使得a!=b不成立退出循環,返回交點指針。
如果大家理解上有困難,可以畫個圖模擬一下。這種想法光是理解對我們來說都挺費勁的,大佬還能用短短几行代碼就將其實現,實在是讓人佩服。
這道題也讓我認識到,自己對指針、鏈表的認識還需要加強。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章