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

 

你可能感兴趣的:(LeetCode)