代码随想录算法训练营第四天
代码随想录算法训练营第四天|24. 两两交换链表中的节点、19.删除链表的倒数第N个节点、面试题 02.07. 链表相交、142.环形链表II
这次四个题之前全部自己刷过,这次看看视频、代码,巩固一下思路。
24.两两交换链表中的节点
/**
 * 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 * d = new ListNode(0);
        d-> next =head;
        ListNode * cur = d;
        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 d->next;
    }
};
一定要画图,不画图想不明白的!
19.删除链表的倒数第N个结点
这道题真是费劲波折。。
第一次尝试
理解错意思了,我以为是删掉值为n的元素。。
/**
 * 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 * dummyHead = new ListNode(0);
        dummyHead -> next = head;
        ListNode * cur =dummyHead;
        while(cur -> next != NULL){
            cur = cur -> next;
            if(cur -> next -> val == n){
                cur -> next = cur -> next -> next;
                return dummyHead -> next;
            }
        }
        return dummyHead -> next;
    }
};
第二次尝试
/**
 * 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 * dummyHead = new ListNode(0);
        dummyHead -> next = head;
        ListNode * cur = dummyHead;
        int size =0;
        int count = 0;
        while(cur -> next != NULL){
            cur = cur -> next;
            size ++;
        }
        cur = dummyHead;
        while(cur -> next != NULL || count < size - n + 1 ){
            cur = cur -> next;
            count ++;
        }
        cur -> next = cur -> next -> next;
        return dummyHead -> next;
    }
};
这段代码报错了
报错的是cur -> next = cur -> next -> next,原因是我尝试访问cur -> next -> next,但在cur -> next为nullptr时会导致访问空指针的成员,这是未定义行为。
第三次:看解析后
实在太妙了!学这个还是得有点数学脑子,其实这道题的难点是如何确定倒数第n个值的位置,这个方法用两个指针一下子就解决了,太巧妙了。
/**
 * 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 *dummyHead = new ListNode(0); // 创建一个虚拟头节点  
        dummyHead->next = head; // 将虚拟头节点指向原链表的头节点  
        ListNode *fast = dummyHead; // fast指针用来先走n步  
        ListNode *slow = dummyHead; // slow指针用来和fast保持一定距离  
          
        // fast先向前移动n步  
        for (int i = 0; i < n + 1; ++i) { // 加1是因为要找到倒数第n个节点的前一个节点  
            fast = fast->next;  
        }  
          
        // 当fast到达链表末尾时,slow就指向了要删除的节点的前一个节点  
        while (fast != nullptr) {  
            fast = fast->next;  
            slow = slow->next;  
        }  
          
        // 删除倒数第n个节点  
        slow->next = slow->next->next;  
          
        // 返回虚拟头节点的下一个节点,即新链表的头节点  
        ListNode* newHead = dummyHead->next;  
          
        // 释放虚拟头节点  
        delete dummyHead;  
          
        return newHead;  
    }  
};
面试题 02.07. 链表相交
暴力破解,双层循环,时间复杂度O(n2)
/**
 * 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;
        while(curA != NULL){
            while(curB != NULL){
                if(curA == curB ){
                    return curB;
                }else{
                    curB = curB -> next;
                }
            }
            curA = curA -> next;
            curB =headB;
        }
        return NULL;
    }
};
142.环形链表
Floyd环检测算法(快慢指针法):
- 
一个快指针(每次移动两步)和一个慢指针(每次移动一步)。如果链表中存在环,那么快慢指针最终会在环中的某个节点相遇。
 - 
如果检测到环,重置一个指针到链表头部,并以相同的速度移动快慢指针,直到它们再次相遇。相遇点即为环的入口节点。
/** * 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 != NULL && fast->next != NULL) { slow = slow->next; fast = fast->next->next; // 快慢指针相遇,此时从head 和 相遇点,同时查找直至相遇 if (slow == fast) { ListNode* index1 = fast; ListNode* index2 = head; while (index1 != index2) { index1 = index1->next; index2 = index2->next; } return index2; // 返回环的入口 } } return NULL; } }; 
代码随想录算法训练 文章被收录于专栏
 代码随想录算法训练
