程式碼隨想錄演算法訓練營day04|24.兩兩交換連結串列中的節點,19.刪除連結串列的倒數第N個節點,面試題 02.07.連結串列相交,142.環形連結串列II

kurumaruq發表於2024-08-06

24.兩兩交換連結串列中的節點

題目連結:https://leetcode.cn/problems/swap-nodes-in-pairs/description/

我的程式碼:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        ListNode* dummy_head = new ListNode;
        dummy_head->next = head;
        ListNode* p = dummy_head;
        while (p->next != nullptr && p->next->next != nullptr) {
            ListNode* temp1 = p->next->next->next;
            ListNode* temp2 = p->next;
            p->next = p->next->next;
            p->next->next = temp2;
            p->next->next->next = temp1;
            p = p->next->next;
        }
        head = dummy_head->next;
        delete dummy_head;
        return head;
    }
};

還是定義虛擬頭節點,同時注意迴圈條件,儲存臨時節點等細節問題。

19.刪除連結串列的倒數第N個節點

題目連結:https://leetcode.cn/problems/remove-nth-node-from-end-of-list/description/

快慢指標法:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode* dummy_head = new ListNode;
        dummy_head->next = head;
        ListNode* fast = dummy_head;
        ListNode* slow = dummy_head;
        while (n--) {
            fast = fast->next;
        }
        while (fast->next) {
            fast = fast->next;
            slow = slow->next;
        }
        ListNode* p = slow->next;
        slow->next = p->next;
        delete p;
        head = dummy_head->next;
        delete dummy_head;
        return head;
    }
};

定義一個虛擬頭節點,先讓快指標走n步,之後兩指標同時走直到fast的next節點為空,此時慢指標指向倒數第n個節點的前一個,即可執行刪除。

面試題 02.07.連結串列相交

題目連結:https://leetcode.cn/problems/intersection-of-two-linked-lists-lcci/description/

我的程式碼:

/**
 * 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* curA = headA;
        ListNode* curB = headB;
        int lengthA = 0;
        int lengthB = 0;
        while (curA != nullptr) {
            lengthA++;
            curA = curA->next;
        }
        while (curB != nullptr) {
            lengthB++;
            curB = curB->next;
        }
        curA = headA;
        curB = headB;
        if (lengthA < lengthB) {
            swap(lengthA, lengthB);
            swap(curA, curB);
        }
        int gap = lengthA - lengthB;
        while (gap--) {
            curA = curA->next;
        }
        while (curA != nullptr) {
            if (curA == curB) {//注意是節點相等不是節點值相等,寫成curA->val == curB->val會答案錯誤
                return curA;
            }
            curA = curA->next;
            curB = curB->next;
        }
        return NULL;//不存在相應節點時別忘了返回NULL
    }
};

求出兩連結串列長度,將A連結串列置為較長連結串列,然後將curA移動到與curB平齊的位置,開始比較。

142.環形連結串列II

題目連結:https://leetcode.cn/problems/linked-list-cycle-ii/description/

我的程式碼:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* detectCycle(ListNode* head) {
        ListNode* fast = head;
        ListNode* slow = head;
        while (fast != nullptr && fast->next != nullptr) {
            fast = fast->next->next;
            slow = slow->next;
            if (fast == slow) {
                ListNode* index1 = head;
                ListNode* index2 = fast;
                while (index1 != index2) {
                    index1 = index1->next;
                    index2 = index2->next;
                }
                return index1;
            }
        }
        return NULL;
    }
};

快慢指標法判斷有無環,fast走兩步,slow走一步,若有環兩指標必相遇。
對環入口位置的數學推導:
Image 1
Image 2

相關文章