题目:
public static class Node {
public int value;
public Node next;
public Node rand;
public Node(int data) {
this.value = data;
}
}
分析:
新的链表也就是说要新开辟一块内存来构建与原链表结构和值相等的新链表,而不是仅仅new 一个Node指向原链表。
这道题目要求在时间复杂度O(n)的情况内完成。而在构建新节点的next和rand时如何快速获得新节点与原节一一对应的关系就是一大难点了。
解法一:使用HashMap结构辅助
能使用辅助结构时用这种方式,特点是简单
在能使用额外数据结构的情况下,对这道题来说用HashMap是很方便的。
因为用HashMap<Node,Node>就能轻松将原链表节点与新链表节点的关系对应起来。
public static Node copyListWithRand1(Node head) {
HashMap<Node, Node> map = new HashMap<Node, Node>();
Node cur = head;
//新节点复制原节点值,并与原节点一一对应地放入HashMap中
while (cur != null) {
map.put(cur, new Node(cur.value));
cur = cur.next;
}
//新节点的next和rand一一对应原节点的next和rand构建
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);
}
解法二:不能用其他数据结构的方法
将复制的新节点放在原节点后
例如原链表是:1==>3==>4==>6==>7
复制后的链表为:1==>1==>3==>3==>4==>4==>6==>6==>7==>7
这样新节点与原节点一一对应的关系就出来了:新节点就是原节点的下一个节点即next
public static Node copyListWithRand2(Node head) {
if(head == null) {
return head;
}
Node curHead = head;
Node curCopy = null;
//在每个节点后面copy一个value相等的新节点
while(curHead != null) {
curCopy = new Node(curHead.value);
curCopy.next = curHead.next;
curHead.next = curCopy;
curHead = curCopy.next;
}
//copy相应的rand指针
curHead = head;
while(curHead != null) {
curCopy = curHead.next;
if(curHead.rand != null) {
curCopy.rand = curHead.rand.next;
}else {
curCopy.rand = null;
}
curHead = curCopy.next;
}
//将copy链表分离出来
Node newHead = head.next;
curHead = head;
curCopy = curHead.next;
while(curHead != null) {
curHead.next = curCopy.next;
if(curCopy.next != null) {
curCopy.next = curCopy.next.next;
curCopy = curCopy.next;
}else {
curCopy.next = null;
}
curHead = curHead.next;
}
return newHead;
}
全代码(含测试代码)
public class 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) {
if(head == null) {
return head;
}
HashMap<Node, Node> map = new HashMap<>();
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 head;
}
Node curHead = head;
Node curCopy = null;
//在每个节点后面copy一个value相等的新节点
while(curHead != null) {
curCopy = new Node(curHead.value);
curCopy.next = curHead.next;
curHead.next = curCopy;
curHead = curCopy.next;
}
//copy相应的rand指针
curHead = head;
while(curHead != null) {
curCopy = curHead.next;
if(curHead.rand != null) {
curCopy.rand = curHead.rand.next;
}else {
curCopy.rand = null;
}
curHead = curCopy.next;
}
//将copy链表分离出来
Node newHead = head.next;
curHead = head;
curCopy = curHead.next;
while(curHead != null) {
curHead.next = curCopy.next;
if(curCopy.next != null) {
curCopy.next = curCopy.next.next;
curCopy = curCopy.next;
}else {
curCopy.next = null;
}
curHead = curHead.next;
}
return newHead;
}
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("=========================");
}
}