2021.10.15 - JZ35.复杂链表的复制

1. 题目

在这里插入图片描述
在这里插入图片描述

2. 思路

(1) 递归+HashMap

  • 利用HashMap存储已创建的结点与原结点的映射关系。
  • 递归得到每个结点的next和random,若HashMap中包含该结点对应的新结点,则直接指向新结点即可,否则创建新结点并存入HashMap中。

(2) 迭代

  • 首先遍历链表,创建的新结点插入原结点的后面,然后再遍历链表,根据原结点的random修改新结点的random,最后拆分两个链表,返回新链表即可。

3. 代码

import java.util.HashMap;
import java.util.Map;

public class Test {
    public static void main(String[] args) {
    }
}

class Node {
    int val;
    Node next;
    Node random;

    public Node(int val) {
        this.val = val;
        this.next = null;
        this.random = null;
    }
}

class Solution {
    private Map<Node, Node> map = new HashMap<>();

    public Node copyRandomList(Node head) {
        if (head == null) {
            return null;
        }
        if (map.containsKey(head)) {
            return map.get(head);
        }
        Node newHead = new Node(head.val);
        map.put(head, newHead);
        newHead.next = copyRandomList(head.next);
        newHead.random = copyRandomList(head.random);
        return newHead;
    }
}

class Solution1 {
    public Node copyRandomList(Node head) {
        if (head == null) {
            return null;
        }
        Node pre = head;
        Node cur;
        while (pre != null) {
            cur = new Node(pre.val);
            cur.next = pre.next;
            pre.next = cur;
            pre = cur.next;
        }
        pre = head;
        while (pre != null) {
            cur = pre.next;
            if (pre.random != null) {
                cur.random = pre.random.next;
            }
            pre = cur.next;
        }
        Node newHead = head.next;
        pre = head;
        while (pre != null) {
            cur = pre.next;
            pre.next = cur.next;
            if (pre.next != null) {
                cur.next = pre.next.next;
            } else {
                cur.next = null;
            }
            pre = pre.next;
        }
        return newHead;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这道题目需要我们实现一个循环链表,并在其中删除指定位置的节点。具体实现可以分为以下几个步骤: 1. 定义循环链表的节点结构体,包括数据域和指向下一个节点的指针域。 2. 定义循环链表的头节点,并初始化为空。 3. 读入输入的数据,将其插入到循环链表的尾部。 4. 遍历循环链表,找到需要删除的节点的前一个节点。 5. 将需要删除的节点从链表中摘除,并释放其内存空间。 6. 遍历循环链表,输出剩余节点的数据。 下面是具体的代码实现: ``` #include <iostream> using namespace std; // 定义循环链表的节点结构体 struct ListNode { int val; ListNode* next; ListNode(int x) : val(x), next(NULL) {} }; int main() { // 定义循环链表的头节点 ListNode* head = NULL; // 读入输入的数据,将其插入到循环链表的尾部 int num; while (cin >> num && num != 0) { ListNode* node = new ListNode(num); if (head == NULL) { head = node; head->next = head; } else { ListNode* cur = head; while (cur->next != head) { cur = cur->next; } cur->next = node; node->next = head; } } // 遍历循环链表,找到需要删除的节点的前一个节点 ListNode* pre = head; while (pre->next != head) { pre = pre->next; } // 将需要删除的节点从链表中摘除,并释放其内存空间 ListNode* cur = head; while (cur->next != head) { if (cur->val % 2 == 0) { pre->next = cur->next; ListNode* temp = cur; cur = cur->next; delete temp; } else { pre = cur; cur = cur->next; } } if (cur->val % 2 == 0) { pre->next = cur->next; delete cur; } // 遍历循环链表,输出剩余节点的数据 cur = head; while (cur->next != head) { cout << cur->val << "\t"; cur = cur->next; } cout << cur->val << endl; return 0; } --相关问题--:

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值