/**
* 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) {
if (head==null) return null;
RandomListNode cur = head;
RandomListNode copycur = null;
RandomListNode copyhead = null;
while (cur!=null) {
copycur = new RandomListNode(cur.label);
if (copyhead==null) copyhead = copycur;
copycur.next = cur.next;
cur.next = copycur;
cur = cur.next.next;
}
cur = head;
while (cur!=null) {
cur.next.random = cur.random!=null?cur.random.next:null;
cur = cur.next.next;
}
cur = head;
while (cur!=null) {
copycur = cur.next;
cur.next = cur.next.next;
copycur.next = cur.next!=null?cur.next.next:null;
cur = cur.next;
}
return copyhead;
}
}
没有评论:
发表评论