CopyListwithRandomPointer

题意:这个题目也是个蛮有意思的题目,就是对一个有随机指针的链表进行深拷贝,思路:简单地来说就是递归拷贝,然后呢防止重复拷贝,所以凡是拷贝过得内存地址都得记录下来代码:

Map<RandomListNode, RandomListNode> m = new HashMap<RandomListNode, RandomListNode>();//保存已经copy的部分public RandomListNode copyRandomList(RandomListNode head) {RandomListNode rList = null;if(head == null) return rList;else {rList = new RandomListNode(head.label);m.put(head, rList);if(m.get(head.next) != null){rList.next = m.get(head.next);}elserList.next = copyRandomList(head.next);if(m.get(head.random) != null) {rList.random = m.get(head.random);}elserList.random = copyRandomList(head.random);}return rList;}

,旅行要学会随遇而安,淡然一点,

CopyListwithRandomPointer

相关文章:

你感兴趣的文章:

标签云: