力扣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个升序链表

思路:迭代法,可以参考之前的合并两个升序链表,不断进行两两合并,比如lists0和lists1合并放在lists0,lists2和lists3合并放在lists2,之后再lists0和lists2合并,以此内推。最终合并的链表都放在lists0,返回即可。

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];
    }
};
相关推荐
ZhengEnCi9 小时前
GPT-6 Astra 爆改 Qwen 权重真有那么神吗:权重空间编程 0.016% 参数的真相
算法·架构
罗西的思考9 小时前
【轻量化复刻大模型 / KIMI K3】KDA(Kimi Delta Attention):并行
人工智能·算法·机器学习
夜不会漫长11 小时前
C++:红黑树
数据结构·算法
文丑颜不良啊11 小时前
LeetCode1661~1680题目解析
java·leetcode
AI备案指南-满满12 小时前
算法备案审核不通过,一般是什么原因?
java·人工智能·算法·大模型备案·算法备案·大模型登记
Ramble_Naylor12 小时前
Rust深度解读:Send 与 Sync 的本质与线程安全边界
python·算法·rust
秋天的怀念_David12 小时前
算法一千零一夜 · 第二十三夜 电力民生写成的曲线
java·程序人生·算法
不一样的故事12612 小时前
NB/T 47013.8-2015 附录D 吸枪技术
人工智能·经验分享·算法
通问AI12 小时前
用 200 行 Python 给文档库做「上下文完整度」自检:四个可复算的检查项
算法
揽星河..12 小时前
【C语言】扫雷游戏
c语言·算法·游戏