LeetCode:Copy List with Random Pointer
2013-10-08 11:22
357 查看
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
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; }
相关文章推荐
- LeetCode | Copy List with Random Pointer
- Copy List with Random Pointer [LeetCode]
- [LeetCode]Copy List with Random Pointer &Clone Graph 复杂链表的复制&图的复制
- leetcode copy list with random pointer
- 【Leetcode】【Hard】Copy List with Random Pointer
- [leetcode]Copy List with Random Pointer
- 【LeetCode with Python】 Copy List with Random Pointer
- leetcode 之 Copy List with Random Pointer
- Copy List with Random Pointer -leetcode
- LeetCode | Copy List with Random Pointer
- leetcode - Copy List with Random Pointer
- leetcode_138 Copy List with Random Pointer
- [LeetCode] Copy List with Random Pointer
- [leetcode]Copy List with Random Pointer @ Python
- LeetCode Copy List with Random Pointer
- 【Leetcode】Copy List with Random Pointer
- LeetCode 14 Copy List with Random Pointer
- [Leetcode] Copy List with Random Pointer (Java)
- LeetCode: Copy List with Random Pointer
- Leetcode:Copy List with Random Pointer