力扣hot100刷题记录(12.1)

29、删除链表倒数第n个节点

思路:使用快慢指针,快指针先走n步,之后快慢指针一起走直到快指针走到底,这时候慢指针指向的便是要删除节点的前一个,进行后一个删除即可。同时注意到有可能第一个节点便是要删除的节点,所以使用一个前置节点指向头节点进行链表存储。

cpp 复制代码
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        int fast = n;
        // 前置节点
        ListNode* pre_head = new ListNode();
        pre_head->next = head;
        ListNode* fast_cur = pre_head, *slow_cur = pre_head;
        while(n--){
            fast_cur = fast_cur->next;
        }
        while(fast_cur->next){
            slow_cur = slow_cur->next;
            fast_cur = fast_cur->next;
        }

        slow_cur->next = slow_cur->next->next;


        return pre_head->next;
    }
};

30、两两交换链表中的节点

思路:递归算法进行节点交换

cpp 复制代码
class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        if(!head || !head->next)
            return head;
        
        ListNode *node1 = head;
        ListNode *node2 = head->next;
        ListNode *node3 = node2->next;

        node1->next = swapPairs(node3);
        node2->next = node1;

        return node2;
    }
};

31、k个一组翻转链表

思路:迭代法,分组翻转链表

cpp 复制代码
class Solution {
    void reverseList(ListNode* head, ListNode *tail){
        ListNode *stop = tail->next;
        ListNode *pre = stop;
        ListNode *cur = head;
        while(cur != stop){
            ListNode *tmp = cur->next;
            cur->next = pre;
            pre = cur;
            cur = tmp;
        }
    }

public:
    ListNode* reverseKGroup(ListNode* head, int k) {
        ListNode *pre_head = new ListNode(0, head), *l = pre_head, *r = pre_head;
        while(1){
            for(int i = 0; i < k; i++){
                r = r->next;
                if(!r) return pre_head->next;
            }
            ListNode *nextl = l->next;
            reverseList(nextl, r);
            l->next = r;
            l = r = nextl;
        }
    }
};

32、随机链表的复制

思路:链表拼接拆分,复制每个节点接入,再拆分。

cpp 复制代码
class Solution {
public:
    Node* copyRandomList(Node* head) {
        if(!head) return nullptr;
        Node *cur = head;
        while(cur){
            Node* tmp = new Node(cur->val);
            tmp->next = cur->next;
            cur->next = tmp;
            cur = tmp->next;
        }

        cur = head;
        while(cur){
            if(cur->random)
                cur->next->random = cur->random->next;
            cur = cur->next->next;
        }

        cur = head->next;
        Node* pre = head, *res = cur;
        while(cur->next){
            pre->next = pre->next->next;
            cur->next = cur->next->next;
            pre = pre->next;
            cur = cur->next;
        }
        pre->next = nullptr;

        return res;
    }
};

33、排序链表

思路:归并排序,采用递归的方式将左侧和右侧链表进行排序合并。

cpp 复制代码
class Solution {
public:
    ListNode* sortList(ListNode* head) {
        if (!head || !head->next) return head;

        ListNode *middle = getMiddle(head);
        ListNode *rightHead = middle->next;
        middle->next = nullptr;

        ListNode *left = sortList(head);
        ListNode *right = sortList(rightHead);

        ListNode *pre_head = new ListNode(0, head);
        ListNode *cur = pre_head;
        while(left && right){
            if(left->val <= right->val){
                cur->next = left;
                left = left->next;
            } else {
                cur->next = right;
                right = right->next;
            }
            cur = cur->next;
        }
        if(left) cur->next = left;
        if(right) cur->next = right;
        return pre_head->next;
    }


    ListNode* getMiddle(ListNode* head) {
        ListNode *slow = head, *fast = head->next;
        while(fast && fast->next){
            slow = slow->next;
            fast = fast->next->next;
        }
        return slow;
    }
};

34、合并k个升序链表

思路:迭代法,可以参考之前的合并两个升序链表,不断进行两两合并,比如lists[0]和lists[1]合并放在lists[0],lists[2]和lists[3]合并放在lists[2],之后再lists[0]和lists[2]合并,以此内推。最终合并的链表都放在lists[0],返回即可。

cpp 复制代码
class Solution {
    ListNode *mergeTowLists(ListNode* list1, ListNode* list2){
        ListNode *pre_head = new ListNode(0);
        ListNode *cur = pre_head;
        while(list1 && list2){
            if(list1->val < list2->val){
                cur->next = list1;
                list1 = list1->next;
            }else{
                cur->next = list2;
                list2 = list2->next;
            }
            cur = cur->next;
        }
        cur->next = list1 ? list1 : list2;
        return pre_head->next;
    }


public:
    ListNode* mergeKLists(vector<ListNode*>& lists) {
        int m = lists.size();
        if(m == 0) return nullptr;
        for(int step = 1; step < m; step *=2) {
            for(int i = 0; i < m - step; i += step * 2){
                lists[i] = mergeTowLists(lists[i], lists[i + step]);
            }
        }
        return lists[0];
    }
};
相关推荐
仰泳的熊猫4 小时前
题目2570:蓝桥杯2020年第十一届省赛真题-成绩分析
数据结构·c++·算法·蓝桥杯
似水明俊德7 小时前
02-C#.Net-反射-面试题
开发语言·面试·职场和发展·c#·.net
无极低码7 小时前
ecGlypher新手安装分步指南(标准化流程)
人工智能·算法·自然语言处理·大模型·rag
软件算法开发7 小时前
基于海象优化算法的LSTM网络模型(WOA-LSTM)的一维时间序列预测matlab仿真
算法·matlab·lstm·一维时间序列预测·woa-lstm·海象优化
superior tigre8 小时前
22 括号生成
算法·深度优先
腾阳9 小时前
99%的人忽视了这一点:活着本身就是人生的意义,别让抑郁和内耗成为你的枷锁!
经验分享·程序人生·职场和发展·跳槽·学习方法·媒体
不吃西红柿的859 小时前
[职场] 内容运营求职简历范文 #笔记#职场发展
笔记·职场和发展·内容运营
liyang_8309 小时前
邦芒秘诀:职场高手都具备的三个特征
职场和发展
普通网友9 小时前
十大秘闻:揭秘霍兰德职业兴趣理论的未知面!
职场和发展·求职招聘·职场发展·单一职责原则