题目 一种特殊的链表节点类描述如下:
public static class Node{
public int value;
public Node next;
public Node random;
public Node(int value){
this.value = value;
}
}
Node类中的value是节点值,next指针和正常单链表中next指针的意义一 样,都指向下一个节点,random指针是Node类中新增的指针,这个指针可能指向链表中的任意一个节点,也可能指向null。 给定一个由 Node节点类型组成的无环单链表的头节点head,请实现一个函数完成这个链表中所有结构的复制,并返回复制的新链表的头节点。
进阶 不使用额外的数据结构,只用有限几个变量,且在时间复杂度为 O(N) 内完成原问题要实现的函数。
思路 (1)使用哈希表实现。时间复杂度为O(1),增删改查时间复杂度都是O(1)。哈希表是K-V结构,即key,value。首先遍历一遍链表,将原链表的节点作为key,该节点的复制版作为key值对应的value,且此时value对应的复制版节点只存放了node.value值,而不知道node.next以及node.random是什么。第二次遍历链表,借用哈希表的key值完善value中节点的node.next以及node.random指针。
(2)【进阶】在原链表上进行操作。首先遍历一次链表,在遍历的同时,将每个节点复制版本插入到链表中对应节点的后面,即 变为
。第二遍遍历,补充复制版节点的node.random值。第三遍遍历,将链表分开,即
变为
和
,返回
即可。
package algorithm.section4;
import java.util.HashMap;
public class CopyListWithRandom {
public static class Node{
public int value;
public Node next;
public Node random;
public Node(int value){
this.value = value;
}
}
public static Node method1(Node head){
if (head == null) return null;
Node cur = head;
HashMap<Node, Node> map = new HashMap<>();
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).random = map.get(cur.random);
cur = cur.next;
}
return map.get(head);
}
public static Node method2(Node head){
if (head == null) return null;
Node cur = head;
while (cur != null){
Node flag = new Node(cur.value);
flag.next = cur.next;
cur.next = flag;
cur = flag.next;
}
cur = head;
while (cur != null){
cur.next.random = cur.random == null ? null : cur.random.next;
cur = cur.next.next;
}
cur = head;
Node next;
Node n = head.next;
while (cur != null){
next = cur.next;
cur.next = next.next;
cur = next.next;
next.next = cur == null ? null : cur.next;
}
return n;
}
public static void print(Node head){
Node cur = head;
System.out.println("\norder: ");
while (cur != null){
System.out.print(cur.value + " ");
cur = cur.next;
}
System.out.println("\nrandom: ");
cur = head;
while (cur != null){
System.out.print(cur.random == null ? "- " : cur.value + "->" + cur.random.value + " ");
cur = cur.next;
}
System.out.println();
}
public static void main(String[] args) {
Node head = null;
Node res1 = null;
Node res2 = null;
print(head);
res1 = method1(head);
print(res1);
res2 = method2(head);
print(res2);
print(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.random = head.next.next.next.next.next; // 1 -> 6
head.next.random = head.next.next.next.next.next; // 2 -> 6
head.next.next.random = head.next.next.next.next; // 3 -> 5
head.next.next.next.random = head.next.next; // 4 -> 3
head.next.next.next.next.random = null; // 5 -> null
head.next.next.next.next.next.random = head.next.next.next; // 6 -> 4
print(head);
res1 = method1(head);
print(res1);
res2 = method2(head);
print(res2);
print(head);
System.out.println("=========================");
}
}