题目描述
输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空)
分析
方法一:先复制结点并连接到原结点之后,再复制随机结点。随机结点的复制方法为:遍历原来的结点,当结点有随机结点时,原结点的下一个结点(即复制结点)的随机结点为原结点的随机结点的next。最后再拆分。
方法二:可以利用哈希表来做,先复制,然后再给next和random赋值。
代码一
public class Main2 {
public RandomListNode copy(RandomListNode pHead){
cloneNode(pHead);
connectRandom(pHead);
return ReConnectNodes(pHead);
}
public void cloneNode(RandomListNode pHead){
RandomListNode node = pHead;
while(node != null){
RandomListNode copyNode = new RandomListNode(node.label);
copyNode.next = node.next;
copyNode.random = null;
node.next = copyNode;
node = copyNode.next;
}
}
public void connectRandom(RandomListNode pHead){
RandomListNode node = pHead;
while(node != null){
RandomListNode clone = node.next;
if(node.random != null){
clone.random = node.random.next;
}
node = clone.next;
}
}
public RandomListNode ReConnectNodes(RandomListNode pHead){
RandomListNode tempNode = pHead;
RandomListNode copyHead = null;
RandomListNode cloneNode = null;
if(tempNode != null){
copyHead = tempNode.next;
cloneNode = tempNode.next;
tempNode.next = cloneNode.next;
tempNode = tempNode.next;
}
while(tempNode != null){
cloneNode.next = tempNode.next;
cloneNode = tempNode.next;
tempNode.next = cloneNode.next;
tempNode = cloneNode.next;
}
return copyHead;
}
public static void main(String[] args) {
RandomListNode head = new RandomListNode(1);
head.next = new RandomListNode(2);
head.next.next = new RandomListNode(3);
head.next.next.next = new RandomListNode(4);
head.next.next.next.next = new RandomListNode(5);
head.random = head.next.next;
head.next.random = head.next.next.next;
head.next.next.random = head.next.next.next.next;
Main2 test = new Main2();
RandomListNode copy = test.copy(head);
while(copy != null){
if(copy.random != null){
System.out.println(copy.label + "," + copy.random.label);
}
System.out.println(copy.label);
copy = copy.next;
}
}
}
class RandomListNode {
int label;
RandomListNode next = null;
RandomListNode random = null;
RandomListNode(int label) {
this.label = label;
}
}
代码二
import java.util.HashMap;
public class RandomListCopy {
public static void main(String[] args) {
// TODO Auto-generated method stub
HashMap<RandomList, RandomList> map = new HashMap<>();
RandomList p1 = new RandomList(null, null,1);
RandomList p2 = new RandomList(null, null,2);
RandomList p3 = new RandomList(null, null,3);
RandomList p4 = new RandomList(null, null,4);
p1.next=p2;
p2.next=p3;
p3.next=p4;
p1.random=p3;
p2.random=p4;
RandomList cHead = copy(p1);
while(cHead!=null){
System.out.println(cHead.val);
if(cHead.random!=null){
System.out.println("Random:"+cHead.random.val);
}
cHead = cHead.next;
}
}
public static RandomList copy(RandomList pHead){
HashMap<RandomList, RandomList> map = new HashMap<>();
RandomList head = pHead;
while(head!=null){
map.put(head, new RandomList(null, null, head.val));
head=head.next;
}
head = pHead;
while(head!=null){
map.get(head).next = map.get(head.next);
map.get(head).random = map.get(head.random);
head=head.next;
}
head = pHead;
RandomList cHead = map.get(head);
return cHead;
}
}
class RandomList{
public int val;
RandomList next;
RandomList random;
public RandomList(RandomList next, RandomList random, int val){
this.next=next;
this.random=random;
this.val=val;
}
}