【20221225】【剑指1】链表

1、从尾到头打印链表

可以用rbegin,rend;也可以用reverse翻转数组。

2、如果是翻转链表的话(双指针法,用虚拟头节点依次翻转):https://blog.csdn.net/HYAIWYH/article/details/127118468?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522167194876116800217017779%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=167194876116800217017779&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~rank_v31_ecpm-3-127118468-null-null.blog_rank_default&utm_term=%E7%BF%BB%E8%BD%AC%E9%93%BE%E8%A1%A8&spm=1018.2226.3001.4450icon-default.png?t=M85Bhttps://blog.csdn.net/HYAIWYH/article/details/127118468?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522167194876116800217017779%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=167194876116800217017779&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~rank_v31_ecpm-3-127118468-null-null.blog_rank_default&utm_term=%E7%BF%BB%E8%BD%AC%E9%93%BE%E8%A1%A8&spm=1018.2226.3001.4450

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

3、复杂链表的复制

给定链表的头节点时,复制链表变得简单,只需要对链表进行遍历,每轮建立新节点 + 构建前驱节点 pre 和当前节点 node 的引用指向即可

本题链表的节点新增了 random 指针,指向链表中的 任意节点 或者 nullnull 。这个 random 指针意味着在复制过程中,除了构建前驱节点和当前节点的引用指向 pre.next ,还要构建前驱节点和其随机节点的引用指向 pre.random 。

遍历复制可以构建next引用指向,但无法构建random引用指向。

如果没有random指针时:

class Solution {
public:
    Node* copyRandomList(Node* head) {
        Node* cur = head;
        Node* dum = new Node(0), *pre = dum;
        while(cur != nullptr) {
            Node* node = new Node(cur->val); // 复制节点 cur
            pre->next = node;                // 新链表的 前驱节点 -> 当前节点
            // pre->random = "???";          // 新链表的 「 前驱节点 -> 当前节点 」 无法确定
            cur = cur->next;                 // 遍历下一节点
            pre = node;                      // 保存当前新节点
        }
        return dum->next;
    }
};

利用哈希表的查询特点,构建 原链表节点 和 新链表对应节点 的键值对映射关系,再遍历构建新链表各节点的 next 和 random 引用指向即可。

算法流程:

class Solution {
public:
    Node* copyRandomList(Node* head) {
        if(head==NULL)  return NULL;
        unordered_map<Node*,Node*> map;
        Node* cur=head;
        while(cur!=NULL)
        {
            map[cur]=new Node(cur->val);
            cur=cur->next;
        }
        cur=head;
        while(cur!=NULL)
        {
            map[cur]->next=map[cur->next];
            map[cur]->random=map[cur->random];
            cur=cur->next;
        }
        return map[head];
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值