【每日一leetcode】Day2 链表(简单)

一、剑指 Offer 06. 从尾到头打印链表

输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。

示例 1:

输入:head = [1,3,2]
输出:[2,3,1]

限制:

0 <= 链表长度 <= 10000

我的解法

使用的方法是最好想的先反转链表再放到数组里。4ms

class Solution {
public:
    vector<int> reversePrint(ListNode* head) {
        vector<int> ans;
        ListNode* p;
        p=nullptr;
        while(head!=nullptr)
        {
            ListNode* tmp=head->next;
            head->next=p;
            p=head;
            head=tmp;
        }
        int i=0;
        while(p!=nullptr)
        {
            ans.push_back(p->val);
            p=p->next;
            
        }
        return ans;
    }
};

开始一直报No viable overloaded '=' 的错,因为将tmp的定义声明写在了跟p一个位置。

其他解法

1、调库

就是直接放到数组后用reverse函数。

class Solution {
public:
    vector<int> reversePrint(ListNode* head) {
        vector<int> ans;
        while(head!=nullptr)
        {
            ans.push_back(head->val);
            head=head->next;
        }
        reverse(ans.begin(),ans.end());
        return ans;
    }
};

2、先遍历一遍找数组大小

遍历完确定数组大小,然后利用rbegin与rend特性(或者倒着减减放入)。

class Solution {
public:
    vector<int> reversePrint(ListNode* head) {
        ListNode* tmp=head;
        int count=0;
        while(tmp!=nullptr)
        {
            count++;
            tmp=tmp->next;
        }
        vector<int> ans(count);
        tmp=head;
        for(auto i=ans.rbegin();i!=ans.rend();i++)
        {
            *i=tmp->val;
            tmp=tmp->next;
        }

        return ans;
    }
};

错误1 error: no viable conversion from 'std::vector<int, std::allocator<int>>::reverse_iterator' (aka 'reverse_iterator<__normal_iterator<int *, std::vector<int, std::allocator<int>>>>') to 'int'

在这里插入图片描述
把int改成auto。

错误2error: no viable overloaded operator[] for type 'vector<int>'
在这里插入图片描述
把ans[i]改为*i。

3、递归

递归好慢啊。。。

class Solution {
public:
    vector<int> ans;
    vector<int> reversePrint(ListNode* head) {
        if(!head)
            return ans;

        reversePrint(head->next);
        ans.push_back(head->val);
        return ans;

    }
};

二、剑指 Offer 35. 复杂链表的复制

请实现 copyRandomList 函数,复制一个复杂链表。在复杂链表中,每个节点除了有一个 next 指针指向下一个节点,还有一个 random 指针指向链表中的任意节点或者 null。

示例 :

在这里插入图片描述

输入:head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
输出:[[7,null],[13,0],[11,4],[10,2],[1,0]]

提示:

-10000 <= Node.val <= 10000
Node.random 为空(null)或指向链表中的节点。
节点数目不超过 1000 。

我的题解

自己能想到的只有深拷贝qaq,最后还是看的题解。

题解

先在每个节点后面复制一遍节点,然后把random节点复制一遍,最后把两个链表拆开,后面的就是题解链表。

class Solution {
public:
    Node* copyRandomList(Node* head) {
        Node* ans;
        if(head==nullptr)
            return nullptr;
        //copy个新的
        for(Node* i=head;i!=nullptr;i=i->next->next)
        {
            Node* copy = new Node(i->val);
            copy->next = i->next;
            i->next = copy;
        }
        //random
        for(Node* i=head;i!=nullptr;i=i->next->next)
        {
            Node* copy = i->next;
            copy->random = (i->random !=nullptr) ? i->random->next : nullptr;
        }

        ans = head->next;
        for(Node* i=head;i!=nullptr;i=i->next)
        {
            Node* copy = i->next;
            i->next = i->next->next;
            copy->next = (copy->next != nullptr) ? copy->next->next : nullptr;
        }

        return ans;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值