/**
* Definition for singly-linked list with a random pointer.
* class RandomListNode {
* int label;
* RandomListNode next, random;
* RandomListNode(int x) { this.label = x; }
* };
*/
public class Solution {
public RandomListNode copyRandomList(RandomListNode head) {
// Note: The Solution object is instantiated only once and is reused by each test case.
//应该是在每个节点后拷贝一份自己的节点,然后将自己的random赋值个自己的next节点
RandomListNode newHead = null;
if(head != null) {
RandomListNode p = head;
while(p != null) {
RandomListNode copyNode = new RandomListNode(p.label);
copyNode.next = p.next;
p.next = copyNode;
p = copyNode.next;
}
p = head;
while(p!= null) {
if(p.random != null) {
p.next.random = p.random.next;
}//p.random = null = =
p = p.next.next;
}
p = head;
newHead = p.next;
RandomListNode newPreP = null;
while (p!=null) {
if(newPreP == null) {
newPreP = p.next;
}
else {
newPreP.next = p.next;
newPreP = p.next;
}
p.next = newPreP.next;
//p.random = null;
p = p.next;
}
}
return newHead;
}
}