• 【力扣】剑指offer第二天


    [剑指 Offer 06. 从尾到头打印链表 - 力扣(LeetCode)]

    link

    image-20221204224450442

    方法一

    递归法

    image-20221206210957442

    把整个链表从尾到头写入到一个数组里,比如,倒序1为头节点的链表,就要先把2为头节点的链表先倒序放到数组中,再把1节点push_back到数组中。2为头节点的链表倒序,就是先把3为头节点的链表倒序放到数组中,再把2节点push_back到数组中,依此类推

    代码

    int reverse_(ListNode* head,vector<int>&tmp)
    {
        if(head->next==NULL)
        {
            return head->val;
        }
        tmp.push_back(reverse_(head->next,tmp));
        return head->val;
    }
    
    class Solution {
    public:
        vector<int> reversePrint(ListNode* head) 
        {
            vector<int> tmp;
            if(head==NULL)
            return tmp;
            tmp.push_back(reverse_(head,tmp));
            return tmp;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    方法二

    遍历链表,将节点的值放进数组中,再使用算法库提供的reverse()函数

    代码

    class Solution {
    public:
        vector<int> reversePrint(ListNode* head) 
        {
            vector<int> tmp;
            ListNode* cur=head;
            while(cur)
            {
                tmp.push_back(cur->val);
                cur=cur->next;
            }
            reverse(tmp.begin(),tmp.end());
            return tmp;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    [剑指 Offer 24. 反转链表 - 力扣(LeetCode)]

    link

    image-20221206212603381

    方法一

    递归法

    image-20221206210957442

    总体思路与上一道题的递归法思路一致

    代码

    class Solution {
    public:
        ListNode* reverseList(ListNode* head) 
        {
               if(head==NULL||head->next==NULL)
               {
                   return head;
               }
               ListNode* newnode=reverseList(head->next);
               head->next->next=head;
               head->next=NULL;
               return newnode;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    方法二

    进入循环前

    image-20221206220620581

    第一次循环结束

    image-20221206220659183

    第二次循环结束

    image-20221206220801399

    第三次循环结束

    image-20221206220840033

    第四次循环结束

    image-20221206220945153

    第五次循环结束

    image-20221206221017590

    代码

    class Solution {
    public:
        ListNode* reverseList(ListNode* head) 
        {
            ListNode* prev=NULL;
            ListNode* cur=head;
            while(cur)
            {
               ListNode* next=cur->next;
                cur->next=prev;
                prev=cur;
                cur=next;
            }
            return prev;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    [剑指 Offer 35. 复杂链表的复制 - 力扣(LeetCode)]

    link

    方法一

    两遍遍历:

    第一遍遍历,创建节点,链接next,并且创建链表的每一个节点时,将被拷贝节点的地址和新创建节点的地址建立哈希

    第二遍遍历,使用哈希表,通过原链表每一个节点中random的内容,映射到新链表对应的节点地址,写入新链表当前节点的random

    代码

    class Solution {
    public:
        Node* copyRandomList(Node* head) 
        {
            if(head==nullptr)
            {
                return nullptr;
            }
    
            unordered_map<Node*,Node*> _map;
            Node* _head=new Node(head->val);
            Node* prev=_head;
            Node* cur=head->next;
            _map[head]=_head;
            while(cur)
            {
                prev->next=new Node(cur->val);
                prev=prev->next;
                _map[cur]=prev;
                cur=cur->next;
            }
            cur=head;
            Node* _cur=_head;
            while(cur)
            {
                _cur->random=_map[cur->random];
                cur=cur->next;
                _cur=_cur->next;
            }
            return _head;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    方法二

    image-20221204220806711

    三遍遍历:

    第一遍遍历

    先将每一个节点,拷贝一份,接到被拷贝节点的后面,

    第二遍遍历

    拷贝节点的random指向的节点就是被拷贝节点的random指向的节点的下一个节点

    第三遍遍历

    拷贝节点拿出来形成一个新从链表

    代码

    class Solution {
    public:
        Node* copyRandomList(Node* head) 
        {
            if(head==nullptr)
            {
                return nullptr;
            }
            unordered_map<Node*,Node*> _map;
            Node* _head=new Node(head->val);
            Node* prev=_head;
            Node* cur=head->next;
            _map[head]=_head;
            while(cur)
            {
                prev->next=new Node(cur->val);
                prev=prev->next;
                _map[cur]=prev;
                cur=cur->next;
            }
            cur=head;
            Node* _cur=_head;
            while(cur)
            {
                _cur->random=_map[cur->random];
                cur=cur->next;
                _cur=_cur->next;
            }
            return _head;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
  • 相关阅读:
    RabbitMQ的工作队列和交换机类型的概念与作用
    python机器学习入门之numpy的用法(超详细,必看)
    Unity扩展UGUI组件多态按钮MultimodeButton
    要学的东西太多,自己能力不足,很焦虑怎么办
    使用JQ获取并渲染三级联动分类数据
    2023年09月 Python(一级)真题解析#中国电子学会#全国青少年软件编程等级考试
    解决kubernetes node节点flannel网卡始终不能添加成功的问题
    探索性测试最佳实践
    详解TCP/IP协议第五篇:详细介绍网络传输中的地址
    JDK8新特性:Stream流
  • 原文地址:https://blog.csdn.net/weixin_53230235/article/details/128211519