算法-面試題系列﹎﹎鏈表問題 ????鏈表相交與環(huán) ????

算法-面試題系列﹎﹎鏈表問題 ????鏈表相交與環(huán) ????

給定兩個(gè)可能有環(huán)也可能無環(huán)的單鏈表,頭節(jié)點(diǎn)head1和head2女揭。請實(shí)現(xiàn)一個(gè)函數(shù)窃植,如果兩個(gè)鏈表相交特石,請返回相交的第一個(gè)節(jié)點(diǎn)。
如果不相交卧秘,返回null
[要求]
如果兩個(gè)鏈表長度之和為N呢袱,時(shí)間復(fù)雜度請達(dá)到O(N),額外空間復(fù)雜度請達(dá)到O(1)翅敌。

怎么樣判斷鏈表有環(huán)羞福?

1-使用HashSet

image-20210519195129198

2-使用快慢指針

  • 結(jié)論一:快慢指針能夠判斷是否有環(huán) 也就是說能夠相遇就代表有環(huán)

  • 結(jié)論二: 將快指針移至起點(diǎn),與慢指針同速行進(jìn)蚯涮,兩者會(huì)在環(huán)入口處相遇治专;


    image-20210519195802466
image-20210519201142207
// 找到鏈表第一個(gè)入環(huán)節(jié)點(diǎn)卖陵,如果無環(huán),返回null
    public static Node getLoopNode(Node head) {
        if (head == null || head.next == null || head.next.next == null) {
            return null;
        }
        // n1 慢  n2 快
        Node slow = head.next; // n1 -> slow
        Node fast = head.next.next; // n2 -> fast
        while (slow != fast) {
            if (fast.next == null || fast.next.next == null) {
                return null;
            }
            fast = fast.next.next;
            slow = slow.next;
        }
        // slow fast  相遇
        fast = head; // n2 -> walk again from head
        while (slow != fast) {
            slow = slow.next;
            fast = fast.next;
        }
        return slow;
    }

現(xiàn)在我們有了getLoopNode函數(shù)张峰,可以判斷鏈表是否有環(huán)的情況我們接下解決兩個(gè)兩個(gè)鏈表相交問題

情況一:兩個(gè)鏈表都是無歡鏈表
image-20210519213619626

兩個(gè)鏈表都是無環(huán)鏈表泪蔫,則end相同則判定相交。

image-20210519214318235
// 如果兩個(gè)鏈表都無環(huán)挟炬,返回第一個(gè)相交節(jié)點(diǎn)鸥滨,如果不想交,返回null
    public static Node noLoop(Node head1, Node head2) {
        if (head1 == null || head2 == null) {
            return null;
        }
        Node cur1 = head1;
        Node cur2 = head2;
        int n = 0;
        while (cur1.next != null) {//計(jì)算長度
            n++;
            cur1 = cur1.next;
        }
        while (cur2.next != null) {//計(jì)算長度
            n--;
            cur2 = cur2.next;
        }
        if (cur1 != cur2) {//end 不相同谤祖,直接返回null 判定無相交節(jié)點(diǎn)婿滓,因?yàn)椴淮嬖谙嘟涣耍缓髢蓚€(gè)又分開的單鏈表結(jié)構(gòu)
            return null;
        }
        // n  :  鏈表1長度減去鏈表2長度的值
        cur1 = n > 0 ? head1 : head2; // 誰長粥喜,誰的頭變成cur1
        cur2 = cur1 == head1 ? head2 : head1; // 誰短凸主,誰的頭變成cur2
        n = Math.abs(n);
        while (n != 0) {//長鏈表先走N步
            n--;
            cur1 = cur1.next;
        }
        while (cur1 != cur2) {//兩個(gè)鏈表一起走。相同停止额湘,返回相交節(jié)點(diǎn)
            cur1 = cur1.next;
            cur2 = cur2.next;
        }
        return cur1;
    }
情況二:兩個(gè)鏈表中卿吐,一個(gè)有環(huán),一個(gè)無環(huán)
image-20210519215023659

這種情況是不可能相交的锋华,可以腦補(bǔ)各種情況嗡官,看看兩個(gè)單鏈表能夠相交否?

情況三:兩個(gè)鏈表都有環(huán)(3中形式)
  • 有環(huán)不相交
image-20210519215258573
  • 相交兩個(gè)鏈表同時(shí)入環(huán)

    這種情況最好區(qū)分毯焕,如果Loop1==Loop2衍腥,說明同時(shí)入環(huán)

    按兩個(gè)鏈表都是無歡鏈表,結(jié)尾為共同點(diǎn)的方式求解第一個(gè)相交點(diǎn)

image-20210519215501586
  • 相交非同一個(gè)入環(huán)節(jié)點(diǎn)

求解方法纳猫,Loop1按環(huán)轉(zhuǎn)一圈婆咸,如果遇到了另一個(gè)節(jié)點(diǎn)Loop2,說明環(huán)里面有L2鏈表芜辕,相交

image-20210519215743710
    // 兩個(gè)有環(huán)鏈表尚骄,返回第一個(gè)相交節(jié)點(diǎn),如果不想交返回null
    public static Node bothLoop(Node head1, Node loop1, Node head2, Node loop2) {
        Node cur1 = null;
        Node cur2 = null;
        if (loop1 == loop2) {//相交兩個(gè)鏈表同時(shí)入環(huán)
            cur1 = head1;
            cur2 = head2;
            int n = 0;
            while (cur1 != loop1) {//計(jì)算走到相交的長度
                n++;
                cur1 = cur1.next;
            }
            while (cur2 != loop2) {//計(jì)算走到相交的長度
                n--;
                cur2 = cur2.next;
            }
            cur1 = n > 0 ? head1 : head2;
            cur2 = cur1 == head1 ? head2 : head1;
            n = Math.abs(n);
            while (n != 0) {//長鏈表先走n步
                n--;
                cur1 = cur1.next;
            }
            while (cur1 != cur2) {//兩個(gè)鏈表一起走侵续。相同停止倔丈,返回相交節(jié)點(diǎn)
                cur1 = cur1.next;
                cur2 = cur2.next;
            }
            return cur1;
        } else {//相交節(jié)點(diǎn)不相同
            cur1 = loop1.next;
            while (cur1 != loop1) {//Loop1按環(huán)轉(zhuǎn)一圈,
                if (cur1 == loop2) {//如果遇到了另一個(gè)節(jié)點(diǎn)Loop2,說明相交状蜗,直接返回
                    return loop1;
                }
                cur1 = cur1.next;
            }
            return null;//環(huán)轉(zhuǎn)了一圈乃沙,沒遇到,不相交
        }
    }

完整代碼

public class FindFirstIntersectNode {

    public static class Node {
        public int value;
        public Node next;

        public Node(int data) {
            this.value = data;
        }
    }

    public static Node getIntersectNode(Node head1, Node head2) {
        if (head1 == null || head2 == null) {
            return null;
        }
        Node loop1 = getLoopNode(head1);
        Node loop2 = getLoopNode(head2);
        if (loop1 == null && loop2 == null) {
            return noLoop(head1, head2);
        }
        if (loop1 != null && loop2 != null) {
            return bothLoop(head1, loop1, head2, loop2);
        }
        return null;
    }

    // 找到鏈表第一個(gè)入環(huán)節(jié)點(diǎn)诗舰,如果無環(huán)警儒,返回null
    public static Node getLoopNode(Node head) {
        if (head == null || head.next == null || head.next.next == null) {
            return null;
        }
        // n1 慢  n2 快
        Node slow = head.next; // n1 -> slow
        Node fast = head.next.next; // n2 -> fast
        while (slow != fast) {
            if (fast.next == null || fast.next.next == null) {
                return null;
            }
            fast = fast.next.next;
            slow = slow.next;
        }
        // slow fast  相遇
        fast = head; // n2 -> walk again from head
        while (slow != fast) {
            slow = slow.next;
            fast = fast.next;
        }
        return slow;
    }

    // 如果兩個(gè)鏈表都無環(huán),返回第一個(gè)相交節(jié)點(diǎn),如果不想交蜀铲,返回null
    public static Node noLoop(Node head1, Node head2) {
        if (head1 == null || head2 == null) {
            return null;
        }
        Node cur1 = head1;
        Node cur2 = head2;
        int n = 0;
        while (cur1.next != null) {
            n++;
            cur1 = cur1.next;
        }
        while (cur2.next != null) {
            n--;
            cur2 = cur2.next;
        }
        if (cur1 != cur2) {
            return null;
        }
        // n  :  鏈表1長度減去鏈表2長度的值
        cur1 = n > 0 ? head1 : head2; // 誰長边琉,誰的頭變成cur1
        cur2 = cur1 == head1 ? head2 : head1; // 誰短,誰的頭變成cur2
        n = Math.abs(n);
        while (n != 0) {
            n--;
            cur1 = cur1.next;
        }
        while (cur1 != cur2) {
            cur1 = cur1.next;
            cur2 = cur2.next;
        }
        return cur1;
    }

    // 兩個(gè)有環(huán)鏈表记劝,返回第一個(gè)相交節(jié)點(diǎn)变姨,如果不想交返回null
    public static Node bothLoop(Node head1, Node loop1, Node head2, Node loop2) {
        Node cur1 = null;
        Node cur2 = null;
        if (loop1 == loop2) {
            cur1 = head1;
            cur2 = head2;
            int n = 0;
            while (cur1 != loop1) {
                n++;
                cur1 = cur1.next;
            }
            while (cur2 != loop2) {
                n--;
                cur2 = cur2.next;
            }
            cur1 = n > 0 ? head1 : head2;
            cur2 = cur1 == head1 ? head2 : head1;
            n = Math.abs(n);
            while (n != 0) {
                n--;
                cur1 = cur1.next;
            }
            while (cur1 != cur2) {
                cur1 = cur1.next;
                cur2 = cur2.next;
            }
            return cur1;
        } else {
            cur1 = loop1.next;
            while (cur1 != loop1) {
                if (cur1 == loop2) {
                    return loop1;
                }
                cur1 = cur1.next;
            }
            return null;
        }
    }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市厌丑,隨后出現(xiàn)的幾起案子定欧,更是在濱河造成了極大的恐慌,老刑警劉巖怒竿,帶你破解...
    沈念sama閱讀 217,542評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件砍鸠,死亡現(xiàn)場離奇詭異,居然都是意外死亡耕驰,警方通過查閱死者的電腦和手機(jī)爷辱,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,822評論 3 394
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來朦肘,“玉大人饭弓,你說我怎么就攤上這事∶娇伲” “怎么了弟断?”我有些...
    開封第一講書人閱讀 163,912評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長趴生。 經(jīng)常有香客問我阀趴,道長,這世上最難降的妖魔是什么冲秽? 我笑而不...
    開封第一講書人閱讀 58,449評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮矩父,結(jié)果婚禮上锉桑,老公的妹妹穿的比我還像新娘。我一直安慰自己窍株,他們只是感情好民轴,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,500評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著球订,像睡著了一般后裸。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上冒滩,一...
    開封第一講書人閱讀 51,370評論 1 302
  • 那天微驶,我揣著相機(jī)與錄音,去河邊找鬼。 笑死因苹,一個(gè)胖子當(dāng)著我的面吹牛苟耻,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播扶檐,決...
    沈念sama閱讀 40,193評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼凶杖,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了款筑?” 一聲冷哼從身側(cè)響起智蝠,我...
    開封第一講書人閱讀 39,074評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎奈梳,沒想到半個(gè)月后杈湾,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,505評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡颈嚼,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,722評論 3 335
  • 正文 我和宋清朗相戀三年毛秘,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片阻课。...
    茶點(diǎn)故事閱讀 39,841評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡叫挟,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出限煞,到底是詐尸還是另有隱情抹恳,我是刑警寧澤,帶...
    沈念sama閱讀 35,569評論 5 345
  • 正文 年R本政府宣布署驻,位于F島的核電站奋献,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏旺上。R本人自食惡果不足惜瓶蚂,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,168評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望宣吱。 院中可真熱鬧窃这,春花似錦、人聲如沸征候。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,783評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽疤坝。三九已至兆解,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間跑揉,已是汗流浹背锅睛。 一陣腳步聲響...
    開封第一講書人閱讀 32,918評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人衣撬。 一個(gè)月前我還...
    沈念sama閱讀 47,962評論 2 370
  • 正文 我出身青樓乖订,卻偏偏與公主長得像,于是被迫代替她去往敵國和親具练。 傳聞我的和親對象是個(gè)殘疾皇子乍构,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,781評論 2 354

推薦閱讀更多精彩內(nèi)容