题解 | #复杂链表的复制#
复杂链表的复制
http://www.nowcoder.com/practice/f836b2c43afc4b35ad6adc41ec941dba
/*
public class RandomListNode {
int label;
RandomListNode next = null;
RandomListNode random = null;
RandomListNode(int label) {
this.label = label;
}
}
*/
public class Solution {
public RandomListNode Clone(RandomListNode pHead) {
if(pHead == null){
return null;
}
Map<RandomListNode,RandomListNode> map = new HashMap<>();
RandomListNode head = pHead;
while(head != null){
map.put(head,new RandomListNode(head.label));
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;
}
return map.get(pHead);
}
}
查看11道真题和解析