拷贝一个存在额外next的单链表

本文介绍了一种特殊单链表的拷贝方法,该链表节点包含next和rand两个指针。通过两种不同策略实现链表的完整复制:使用哈希映射和穿插复制节点的方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

拷贝一个存在额外next的单链表

package class_03;

import java.util.HashMap;

public class Code_13_CopyListWithRandom {

    public static class Node {
        public int value;
        public Node next;
        public Node rand;

        public Node(int data) {
            this.value = data;
        }
    }

    public static Node copyListWithRand1(Node head) {
        HashMap<Node, Node> map = new HashMap<Node, Node>();
        Node cur = head;
        while (cur != null) {
            map.put(cur, new Node(cur.value));
            cur = cur.next;
        }
        cur = head;
        while (cur != null) {
            map.get(cur).next = map.get(cur.next);
            map.get(cur).rand = map.get(cur.rand);
            cur = cur.next;
        }
        return map.get(head);
    }

    public static Node copyListWithRand2(Node head) {
        if (head == null) {
            return null;
        }
        Node cur = head;
        Node next = null;
        // copy node and link to every node
        while (cur != null) {
            next = cur.next;
            cur.next = new Node(cur.value);
            cur.next.next = next;
            cur = next;
        }
        cur = head;
        Node curCopy = null;
        // set copy node rand
        while (cur != null) {
            next = cur.next.next;
            curCopy = cur.next;
            curCopy.rand = cur.rand != null ? cur.rand.next : null;
            cur = next;
        }
        Node res = head.next;
        cur = head;
        // split
        while (cur != null) {
            next = cur.next.next;
            curCopy = cur.next;
            cur.next = next;
            curCopy.next = next != null ? next.next : null;
            cur = next;
        }
        return res;
    }

    public static void printRandLinkedList(Node head) {
        Node cur = head;
        System.out.print("order: ");
        while (cur != null) {
            System.out.print(cur.value + " ");
            cur = cur.next;
        }
        System.out.println();
        cur = head;
        System.out.print("rand:  ");
        while (cur != null) {
            System.out.print(cur.rand == null ? "- " : cur.rand.value + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public static void main(String[] args) {
        Node head = null;
        Node res1 = null;
        Node res2 = null;
        printRandLinkedList(head);
        res1 = copyListWithRand1(head);
        printRandLinkedList(res1);
        res2 = copyListWithRand2(head);
        printRandLinkedList(res2);
        printRandLinkedList(head);
        System.out.println("=========================");

        head = new Node(1);
        head.next = new Node(2);
        head.next.next = new Node(3);
        head.next.next.next = new Node(4);
        head.next.next.next.next = new Node(5);
        head.next.next.next.next.next = new Node(6);

        head.rand = head.next.next.next.next.next; // 1 -> 6
        head.next.rand = head.next.next.next.next.next; // 2 -> 6
        head.next.next.rand = head.next.next.next.next; // 3 -> 5
        head.next.next.next.rand = head.next.next; // 4 -> 3
        head.next.next.next.next.rand = null; // 5 -> null
        head.next.next.next.next.next.rand = head.next.next.next; // 6 -> 4

        printRandLinkedList(head);
        res1 = copyListWithRand1(head);
        printRandLinkedList(res1);
        res2 = copyListWithRand2(head);
        printRandLinkedList(res2);
        printRandLinkedList(head);
        System.out.println("=========================");

    }

}
在C语言中,你可以通过遍历原链表并逐个节点添加到新链表中来实现单链表的复制。这里是一个简单的函数示例: ```c #include <stdio.h> #include <stdlib.h> // 定义链表节点结构 typedef struct Node { int data; struct Node* next; } Node; // 创建新节点 Node* createNode(int data) { Node* newNode = (Node*)malloc(sizeof(Node)); if (newNode != NULL) { newNode->data = data; newNode->next = NULL; } return newNode; } // 复制链表 Node* copyList(Node* head) { if (head == NULL) { return NULL; // 如果链表为空,直接返回NULL } Node* newHead = createNode(head->data); // 复制第一个节点 Node* current = head; while (current != NULL) { // 遍历原链表 Node* newNode = createNode(current->data); newNode->next = newHead->next; // 新节点的下一个指针指向旧链表的新位置 newHead->next = newNode; // 更新新链表的头节点 newHead = newHead->next; // 移动新链表头部指针 current = current->next; } return newHead; } // 打印链表 void printList(Node* head) { while (head != NULL) { printf("%d ", head->data); head = head->next; } printf("\n"); } int main() { // 实例化一个链表并创建它的副本 Node* originalList = ... // 初始化你的链表 Node* copiedList = copyList(originalList); // 打印原链表和复制后的链表 printList(originalList); printList(copiedList); return 0; } ``` 这个`copyList`函数首先检查链表是否为空,然后创建一个新的头节点,接着遍历原链表,每次迭代都会创建一个新的节点,并将其插入到新链表的适当位置。当遍历完原链表后,函数返回新链表的头节点。 注意:这个例子假设你已经有了初始化好的`originalList`链表,你需要根据实际链表的构建来替换这部分代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值