A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.
Return a deep copy of the list.
看了微信公众账号待字闺中的答案:
http://mp.weixin.qq.com/mp/appmsg/show?__biz=MjM5ODIzNDQ3Mw==&appmsgid=10000291&itemidx=1&sign=ccde63918a24dee181f1fd1a4e3e6781
public RandomListNode copyRandomList(RandomListNode head) {
if (head == null) {
return null;
}
RandomListNode node = head;
RandomListNode temp = null;
//insert new node
while (node != null) {
temp = node.next;
RandomListNode newNode = new RandomListNode(node.label);
node.next = newNode;
newNode.next = temp;
node = temp;
}
//update new node's random
node = head;
while (node != null) {
if (node.random != null) {
node.next.random = node.random.next;
}
node = node.next.next;
}
RandomListNode newHead = head.next;
node = head;
RandomListNode newNode = node.next;
while (node != null) {
node.next = newNode.next;
if (newNode.next != null) {
newNode.next = newNode.next.next;
}
node = node.next;
if (node != null) {
newNode = node.next;
}
}
return newHead;
}