leetcode---copy-list-with-random-pointer---链表
2017-10-29 13:57
447 查看
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.
/** * Definition for singly-linked list with a random pointer. * struct RandomListNode { * int label; * RandomListNode *next, *random; * RandomListNode(int x) : label(x), next(NULL), random(NULL) {} * }; */ class Solution { public: RandomListNode *copyRandomList(RandomListNode *head) { if(!head) return head; RandomListNode *next = NULL, *p = head; while(p) { next = p->next; RandomListNode *node = new RandomListNode(p->label); node->next = p->next; p->next = node; p = next; } p = head; while(p) { if(p->random) p->next->random = p->random->next; p = p->next->next; } RandomListNode *next2 = NULL, *newH = NULL, *newP = NULL; p = head; while(p) { if(!newH) { next2 = p->next->next; newH = p->next; newP = newH; p->next = next2; p = next2; } else { next2 = p->next->next; newP->next = p->next; p->next = next2; p = next2; newP = newP->next; } } return newH; } };
相关文章推荐
- (不会)[LeetCode] Copy List with Random Pointer 拷贝带有随机指针的链表
- [LeetCode] Copy List with Random Pointer 拷贝带有随机指针的链表
- [LeetCode]Copy List with Random Pointer &Clone Graph 复杂链表的复制&图的复制
- LeetCode之复制有random指针的链表Copy List with Random Pointer
- LeetCode138 Copy List with Random Pointer(深度复制带有随机指针的链表) Java题解
- 【LeetCode-面试算法经典-Java实现】【138-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
- 【LeetCode-面试算法经典-Java实现】【143-Copy List with Random Pointer(有随机指针的链表复制)】
- [LeetCode]Copy List with Random Pointer &Clone Graph 复杂链表的复制&图的复制
- LeetCode——Copy List with Random Pointer(带random引用的单链表深拷贝)
- LeetCode(Copy List with Random Pointer) 复杂链表的深拷贝
- [LeetCode] Copy List with Random Pointer
- Java for LeetCode 138 Copy List with Random Pointer
- leetcode -day8 Copy List with Random Pointer & Single Number I II
- LeetCode "Copy List with Random Pointer"