力扣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];
    }
};
相关推荐
先吃饱再说11 小时前
判断回文字符串,从一行代码到双指针优化
算法
黄敬峰13 小时前
深入理解算法核心:从递归思想、数组扁平化到快速排序
算法
得物技术15 小时前
从狂野代码到按目标生产:得物推荐 AI Harness 的工程化实践|AICon 演讲整理
人工智能·算法·架构
AI小老六18 小时前
SkillOpt 架构拆解:把 Skill 文本当参数,用执行轨迹训练 Agent
后端·算法·ai编程
胡萝卜术19 小时前
从“分数打架”到“排名投票”:为什么你的ChatBI必须用RRF?
算法·设计模式·面试
Asize19 小时前
初识DFS 与 BFS:递归、队列与图遍历
算法
罗西的思考1 天前
机器人 / 强化学习】HIL-SERL:人类在环驱动的具身智能进化框架
人工智能·算法·机器学习
美团技术团队2 天前
LongCat 开源 VitaBench 2.0:长期动态智能体基准新标杆
人工智能·算法
To_OC2 天前
LC 207 课程表:刚学图论那会儿,我连这是拓扑排序都没看出来
javascript·算法·leetcode