国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

代碼隨想錄刷題第4天|LeetCode24、LeetCode19、LeetCode160、LeetCode142

這篇具有很好參考價(jià)值的文章主要介紹了代碼隨想錄刷題第4天|LeetCode24、LeetCode19、LeetCode160、LeetCode142。希望對大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

1、LeetCode24 兩兩交換鏈表中的節(jié)點(diǎn)

題目鏈接:24、兩兩交換鏈表中的節(jié)點(diǎn)

代碼隨想錄刷題第4天|LeetCode24、LeetCode19、LeetCode160、LeetCode142

要想清楚終止條件,cur每次指向要交換的兩個(gè)節(jié)點(diǎn)的前一個(gè)節(jié)點(diǎn),cur = cur->next->next;

若鏈表元素個(gè)數(shù)為偶數(shù) , 則最后時(shí)刻 cur->next = NULL;

若鏈表元素個(gè)數(shù)為奇數(shù),則最后時(shí)刻 cur->next->next = NULL;

最后要返回 dummyHead->next, 因?yàn)榻粨Q節(jié)點(diǎn)后 head 為 “2”,不是原來的 “1”。

class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        ListNode * dummyHead = new ListNode(0);
        dummyHead->next = head;
        ListNode * cur = dummyHead;

        while(cur->next!=NULL && cur->next->next!=NULL)
        {
            ListNode * temp = cur->next;
            ListNode * temp1 = cur->next->next->next;

            cur->next = cur->next->next;
            cur->next->next = temp;
            cur->next->next->next = temp1;

            cur = cur->next->next;
        }

        return dummyHead->next;
    }
};

2、LeetCode19 刪除鏈表的倒數(shù)第N個(gè)節(jié)點(diǎn)

題目鏈接:19、刪除鏈表的倒數(shù)第N個(gè)節(jié)點(diǎn)

用快慢指針,快指針先向后移動(dòng)N+1個(gè)節(jié)點(diǎn),然后當(dāng)快指針不為NULL時(shí),快慢指針一起向后移動(dòng),快指針為NULL時(shí),慢指針剛好移動(dòng)到 倒數(shù)第N個(gè)節(jié)點(diǎn)的前一個(gè)節(jié)點(diǎn)。

class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode * dummyHead = new ListNode(0);
        dummyHead->next = head;
        ListNode * fast = dummyHead;
        ListNode * slow = dummyHead;

        n++;
        while(n-- && fast!=NULL)
        {
            fast = fast->next;
        }
        while(fast!=NULL)
        {
            fast = fast->next;
            slow = slow->next;
        }

        ListNode * temp = slow->next;
        slow->next = slow->next->next;
        delete temp;

        return dummyHead->next;
    }
};

3、LeetCode160 鏈表相交

題目鏈接:160 鏈表相交

代碼隨想錄刷題第4天|LeetCode24、LeetCode19、LeetCode160、LeetCode142

class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        ListNode * cura = headA;
        ListNode * curb = headB;
        int lenA = 0;
        int lenB = 0;

        while(cura)
        {
            lenA++;
            cura = cura->next;
        }
        while(curb)
        {
            lenB++;
            curb = curb->next;
        }
        cura = headA;
        curb = headB;
        
        // 讓curA為最長鏈表的頭,lenA為其長度
        if (lenB > lenA)
        {
            swap(lenA,lenB);
            swap(cura,curb);
        }

        //求長度差
        int gap = lenA - lenB;
        while (gap--)
        {
            cura = cura->next;
        }

        while(cura)
        {
            if (cura == curb)
            {
                return cura;
            }
            cura = cura->next;
            curb = curb->next;
        }
        return NULL;
    }
};

4、LeetCode142 環(huán)形鏈表

題目鏈接:142、環(huán)形鏈表

找相遇節(jié)點(diǎn),相遇節(jié)點(diǎn)和頭結(jié)點(diǎn)同時(shí)出發(fā),相遇時(shí)即為環(huán)形入口節(jié)點(diǎn)。(太厲害了,自己是想不出來。)

代碼隨想錄刷題第4天|LeetCode24、LeetCode19、LeetCode160、LeetCode142文章來源地址http://www.zghlxwxcb.cn/news/detail-454670.html

class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        ListNode * fast = head;
        ListNode * slow = head;

        while (fast!=NULL && fast->next!=NULL)
        {

            slow = slow->next;
            fast = fast->next->next;
            if(slow == fast)
            {
                ListNode * index1 = head;
                ListNode * index2 = fast;
                while (index1 != index2)
                {
                    index1 = index1->next;
                    index2 = index2->next;
                }
                return index1;
            }
        }
        return NULL;
    }
};

到了這里,關(guān)于代碼隨想錄刷題第4天|LeetCode24、LeetCode19、LeetCode160、LeetCode142的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包