138. Copy List with Random Pointer
来源:互联网 发布:网络运维学习 编辑:程序博客网 时间:2024/06/06 20:49
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.
/** * Definition for singly-linked list with a random pointer. * struct RandomListNode { * int label; * struct RandomListNode *next; * struct RandomListNode *random; * }; */struct RandomListNode *copyRandomList(struct RandomListNode *head) { struct RandomListNode *oldNode = head; struct RandomListNode *newNode = NULL; struct RandomListNode *newHead = NULL; if(head == NULL){ return NULL; } /*insert a new randomList Node*/ while(oldNode){ newNode = (struct Random *)malloc(sizeof(struct RandomListNode)); if(NULL == newNode){ return NULL; } newNode->label = oldNode->label; newNode->next = oldNode->next; newNode->random = oldNode->random; oldNode->next = newNode; oldNode = newNode->next; } /*link the new List Node*/ oldNode = head; while(oldNode){ if(oldNode->random != NULL){ oldNode->next->random = oldNode->random->next; } if(oldNode->next!=NULL){ oldNode = oldNode->next->next; } } /*link the new List Node from the old List*/ oldNode = head; newHead = head->next; while(oldNode){ newNode = oldNode->next; oldNode->next = newNode->next; oldNode = oldNode->next; if(oldNode != NULL){ newNode->next = oldNode->next; } } return newHead;}
阅读全文
0 0
- 138. Copy List with Random Pointer
- LeetCode 138. Copy List with Random Pointer
- [Leetcode] 138. Copy List with Random Pointer
- 138. Copy List with Random Pointer
- LeetCode 138. Copy List with Random Pointer
- [leetcode] 138. Copy List with Random Pointer
- LeetCode 138. Copy List with Random Pointer
- 138. Copy List with Random Pointer
- LeetCode 138. Copy List with Random Pointer
- 138. Copy List with Random Pointer
- leetcode.138. Copy List with Random Pointer
- 138. Copy List with Random Pointer
- LeetCode-138.Copy List with Random Pointer
- Leetcode 138. Copy List with Random Pointer
- [Leetcode]138. Copy List with Random Pointer
- LeetCode 138. Copy List with Random Pointer
- 138. Copy List with Random Pointer
- [LeetCode] 138. Copy List with Random Pointer
- 前端
- 第一题 two-sum
- 移动端选择日期
- FIleInputStream中read和Socket中read源码分析
- Java堆、栈和常量池以及相关String的详细讲解(经典中的经典)
- 138. Copy List with Random Pointer
- Oracle优化——三种联结方法
- Heroku部署Rails应用流程
- Hi,我们再来聊一聊Java的单例吧
- 杂乱的东西
- 常见排序算法
- MyBatis学习(一)-HelloWorld
- Effective Objective-C 2.0 读书笔记 (一)
- Effective Objective-C 2.0 读书笔记 (二)