• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
ying_vincent
博客园    首页    新随笔    联系   管理    订阅  订阅

LeetCode: Copy List with Random Pointer

跟clone graph思路一样

 1 /**
 2  * Definition for singly-linked list with a random pointer.
 3  * struct RandomListNode {
 4  *     int label;
 5  *     RandomListNode *next, *random;
 6  *     RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     RandomListNode *copyRandomList(RandomListNode *head) {
12         // IMPORTANT: Please reset any member data you declared, as
13         // the same Solution instance will be reused for each test case.
14         if (head == NULL) return NULL;
15         map<RandomListNode*, RandomListNode*> S;
16         RandomListNode *res = new RandomListNode(head->label);
17         S[head] = res;
18         queue<RandomListNode *> que;
19         que.push(head);
20         while (!que.empty()) {
21             RandomListNode *top = que.front();
22             que.pop();
23             if (top->next) {
24                 RandomListNode *next = new RandomListNode(top->next->label);
25                 S[top->next] = next;
26                 S[top]->next = next;
27                 que.push(top->next);
28             }
29             if (top->random) {
30                 RandomListNode *tmp = top->random;
31                 if (S.count(tmp)) S[top]->random = S[tmp];
32                 else {
33                     RandomListNode *random = new RandomListNode(tmp->label);
34                     S[tmp] = random;
35                     S[top]->random = random;
36                 }
37             }
38         }
39         return res;
40     }
41 };

 C#

 1 /**
 2  * Definition for singly-linked list with a random pointer.
 3  * public class RandomListNode {
 4  *     public int label;
 5  *     public RandomListNode next, random;
 6  *     public RandomListNode(int x) { this.label = x; }
 7  * };
 8  */
 9 public class Solution {
10     public RandomListNode CopyRandomList(RandomListNode head) {
11         if (head == null) return null;
12         Dictionary<RandomListNode, RandomListNode> S = new Dictionary<RandomListNode, RandomListNode>();
13         RandomListNode ans = new RandomListNode(head.label);
14         S.Add(head, ans);
15         Queue<RandomListNode> que = new Queue<RandomListNode>();
16         que.Enqueue(head);
17         while (que.Count != 0) {
18             RandomListNode peek = que.Peek();
19             que.Dequeue();
20             if (peek.next != null) {
21                 RandomListNode next = new RandomListNode(peek.next.label);
22                 if (S.ContainsKey(peek.next)) S[peek.next] = next;
23                 else S.Add(peek.next, next);
24                 S[peek].next = next;
25                 que.Enqueue(peek.next);
26             }
27             if (peek.random != null) {
28                 RandomListNode tmp = peek.random;
29                 if (S.ContainsKey(tmp)) S[peek].random = S[tmp];
30                 else {
31                     RandomListNode random = new RandomListNode(tmp.label);
32                     S[tmp] = random;
33                     S[peek].random = random;
34                 }
35             }
36         }
37         return ans;
38     }
39 }
View Code

 

posted @ 2013-11-04 11:02  ying_vincent  阅读(182)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3