代码随想录算法训练营第4天 | 24. 两两交换链表中的节点 , 19.删除链表的倒数第N个节点 ,面试题 02.07. 链表相交 ,142.环形链表II

今天的温习主要是针对链表的特定题型,继续复习巩固链表的特点和有趣之处

要点:熟练画图,虚拟头节点(这两个几乎每次都有), 模拟,双指针(快慢指针)

24. 两两交换链表中的节点

24. 两两交换链表中的节点 - 力扣(LeetCode)

这一题很容易想到虚拟节点,毕竟头节点会参与计算,需要注意的是节点数奇偶会不一样,要注意不能对nullptr做next和val的操作。

/**
 * 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) {
        if(head == nullptr) return head;
        ListNode* dummy = new ListNode(0);   // 记得带上头
        dummy->next = head;
        ListNode* prev = dummy;     // 记住以前的 
        ListNode* node = head;      // 这是现在的
        while(node != nullptr && node->next != nullptr){  // 奇偶情况都要考虑
            prev->next = node->next;      // 
            node->next = node->next->next;  // 
            prev->next->next = node;
            prev = node;
            node = node->next;
        }
        ListNode *res = dummy->next;
        delete dummy;
        return res;
    }
};

19.删除链表的倒数第N个节点

19. 删除链表的倒数第 N 个结点 - 力扣(LeetCode)

我是直接暴力求解,先算总长,再顺序删除,使用双指针确实更方便高效

/**
 * 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;
        int sz = 0;
        ListNode* node = head;
        while(node != nullptr){
            node = node->next;
            sz++;
        }
        node = dummyHead;
        for(int i = 0; i < sz - n; i++){
            node = node->next;
        }
        ListNode* del = node->next;
        node->next = node->next->next;
        ListNode* res = dummyHead->next;
        delete del, dummyHead;
        return res;
    }
};

面试题 02.07. 链表相交

面试题 02.07. 链表相交 - 力扣(LeetCode)

我使用的是双指针,有交点则交换遍历就会相遇,关键在于什么时候停止,不过训练营的想法更加

聪明,充分利用了相交链表的后段会一模一样的性质

/**
 * 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 == nullptr || headB == nullptr){
            return nullptr;
        }
        ListNode *mNode = headA;
        ListNode *nNode = headB;
        while(mNode != nNode){
            if(mNode->next == nullptr && nNode->next == nullptr){
                return nullptr;
            }else if(mNode->next == nullptr){
                mNode = headB;
                nNode = nNode->next;
            }else if(nNode->next == nullptr){
                nNode = headA;
                mNode = mNode->next;
            }else{
                mNode = mNode->next;
                nNode = nNode->next;              
            }
        }
        return mNode;
    }
};

 142.环形链表II 

142. 环形链表 II - 力扣(LeetCode)

让我想起跑步被人套圈的经历,快慢指针,对于环形的理解,涉及物理,学会画图会好理解很多 

/**
 * 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) {
        if(head == nullptr) return nullptr;
        ListNode *slow = head, *fast = head;
        while(fast != nullptr){
            slow = slow->next;
            if(fast->next != nullptr){
                fast = fast->next->next;
            }else{
                return nullptr;
            }
            if(slow == fast){
                ListNode *start = head;
                while(slow != start){
                    slow = slow->next;
                    start = start->next;
                }
                return slow;
            }
        }
        return nullptr;
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值