一种特殊的单链表节点类多了rand指针,可能指向链表中的任一点,也可能指向null。给定一个无环单链表的头节点head,用一个函数完成这个链表的复制。返回新链表头节点(时间O(1),空间O(N))。

该代码实现了一个Java方法,用于复制一个具有随机指针的链表。方法包括两种策略:一是使用HashMap存储原始节点及其副本,二是通过复制节点并重新链接来创建新链表。这两种方法都在代码中进行了详细说明和测试。
摘要由CSDN通过智能技术生成
package class06;

import java.util.HashMap;

public class Code04_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) {
         // cur 老
         // map.get(cur) 新
         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
      // 1 -> 2
      // 1 -> 1' -> 2
      while (cur != null) {
         // cur 老   next 老的下一个
         next = cur.next;
         cur.next = new Node(cur.value);
         cur.next.next = next;
         cur = next;
      }
      cur = head;
      Node curCopy = null;
      // set copy node rand
      // 1 -> 1' -> 2 -> 2'
      while (cur != null) {
         // cur 老
         // cur.next  新 copy

         next = cur.next.next;
         curCopy = cur.next;
         curCopy.rand = cur.rand != null ? cur.rand.next : null;
         cur = next;
//       cur.next.rand = cur.rand != null ? cur.rand.next: null;
//       cur = cur.next.next;
      }
      // head  head.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;
//       cur.next = cur.next != null ? cur.next.next : null;
//       cur = cur.next.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("=========================");

   }

}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值