【力扣】剑指offer第二天
创始人
2024-04-18 18:06:56

剑指offer第二天

    • [剑指 Offer 06. 从尾到头打印链表 - 力扣(LeetCode)]
      • 方法一
      • 代码
      • 方法二
      • 代码
    • [剑指 Offer 24. 反转链表 - 力扣(LeetCode)]
      • 方法一
      • 代码
      • 方法二
      • 代码
    • [剑指 Offer 35. 复杂链表的复制 - 力扣(LeetCode)]
      • 方法一
      • 代码
      • 方法二
      • 代码

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

link

image-20221204224450442

方法一

递归法

image-20221206210957442

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

代码

int reverse_(ListNode* head,vector&tmp)
{if(head->next==NULL){return head->val;}tmp.push_back(reverse_(head->next,tmp));return head->val;
}class Solution {
public:vector reversePrint(ListNode* head) {vector tmp;if(head==NULL)return tmp;tmp.push_back(reverse_(head,tmp));return tmp;}
};

方法二

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

代码

class Solution {
public:vector reversePrint(ListNode* head) {vector tmp;ListNode* cur=head;while(cur){tmp.push_back(cur->val);cur=cur->next;}reverse(tmp.begin(),tmp.end());return tmp;}
};

[剑指 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;}
};

方法二

进入循环前

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;}
};

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

link

方法一

两遍遍历:

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

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

代码

class Solution {
public:Node* copyRandomList(Node* head) {if(head==nullptr){return nullptr;}unordered_map _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;}
};

方法二

image-20221204220806711

三遍遍历:

第一遍遍历

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

第二遍遍历

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

第三遍遍历

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

代码

class Solution {
public:Node* copyRandomList(Node* head) {if(head==nullptr){return nullptr;}unordered_map _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;}
};

相关内容

热门资讯

应用未安装解决办法 平板应用未... ---IT小技术,每天Get一个小技能!一、前言描述苹果IPad2居然不能安装怎么办?与此IPad不...
脚上的穴位图 脚面经络图对应的... 人体穴位作用图解大全更清晰直观的标注了各个人体穴位的作用,包括头部穴位图、胸部穴位图、背部穴位图、胳...
demo什么意思 demo版本... 618快到了,各位的小金库大概也在准备开闸放水了吧。没有小金库的,也该向老婆撒娇卖萌服个软了,一切只...
世界上最漂亮的人 世界上最漂亮... 此前在某网上,选出了全球265万颜值姣好的女性。从这些数量庞大的女性群体中,人们投票选出了心目中最美...