Copy List with Random Pointer
2016-08-19 03:15
316 查看
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.
Return a deep copy of the list.
RandomListNode* result; if (!head) return result; // copy each node in the original list RandomListNode* move = head; while (move) { RandomListNode* temp = new RandomListNode(move->label); temp->next = move->next; move->next = temp; move = move->next->next; } // specify the random pointer of each new node move = head; while (move) { if (move->random) move->next->random = move->random->next; move = move->next->next; } // break the list into two and return the new one move = head; RandomListNode* newHead = head->next; RandomListNode* moveNew = newHead; while (move) { move->next = move->next ? move->next->next : NULL; moveNew->next = moveNew->next ? moveNew->next->next : NULL; move = move->next; moveNew = moveNew->next; } return newHead;
相关文章推荐
- Copy List with Random Pointer
- Copy List with Random Pointer
- LeetCode题解:Copy List with Random Pointer
- leetcode(十一)Copy List with Random Pointer
- [leetCode] Copy List with Random Pointer
- 【copy-list-with-random-pointer】
- leetcode--011 copy list with random pointer
- [leetcode]Copy List with Random Pointer
- leetcode之Copy List with Random Pointer
- [leetcode]Copy List with Random Pointer
- [LeetCode]Copy List with Random Pointer
- LeetCode | Copy List with Random Pointer(赋值带有随机指针的链表)
- 难题部分Copy List with Random Pointer
- leetcode 之Copy List with Random Pointer(23)
- LeetCode 14 Copy List with Random Pointer
- 【leetcode】Copy List with Random Pointer
- Leetcode: Copy List with Random Pointer - 再解
- LeetCode | Copy List with Random Pointer
- Copy List with Random Pointer
- [leetcode] Copy List with Random Pointer, Java