-
Notifications
You must be signed in to change notification settings - Fork 171
/
CopyListwithRandomPointer.java
111 lines (110 loc) · 4.19 KB
/
CopyListwithRandomPointer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
Author: Andy, [email protected]
Date: Jan 13, 2015
Problem: Copy List with Random Pointer
Difficulty: Easy
Source: http://oj.leetcode.com/problems/copy-list-with-random-pointer/
Notes:
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.
Solution: 1. HashMap
2. uses constant extra space.
3. Recursive. (Stack)-->StackOverflow in Java.
4. Iterative. (Queue)
*/
/**
* Definition for singly-linked list with a random pointer.
* class RandomListNode {
* int label;
* RandomListNode next, random;
* RandomListNode(int x) { this.label = x; }
* };
*/
public class Solution {
public RandomListNode copyRandomList_1(RandomListNode head) {
if (head == null) return null;
HashMap<RandomListNode, RandomListNode> map = new HashMap<RandomListNode, RandomListNode>();
RandomListNode dummy = new RandomListNode(-1);
RandomListNode curNew = dummy, cur = head;
while (cur != null) {
if (map.containsKey(cur) == false) {
map.put(cur, new RandomListNode(cur.label));
}
if (cur.random != null && map.containsKey(cur.random) == false) {
map.put(cur.random, new RandomListNode(cur.random.label));
}
curNew.next = map.get(cur);
curNew.next.random = map.get(cur.random);
curNew = curNew.next;
cur = cur.next;
}
return dummy.next;
}
public RandomListNode copyRandomList_2(RandomListNode head) {
if (head == null) return null;
RandomListNode cur = head;
while (cur != null) {
RandomListNode newnode = new RandomListNode(cur.label);
newnode.next = cur.next;
cur.next = newnode;
cur = cur.next.next;
}
cur = head;
while (cur != null) {
if (cur.random != null) {
cur.next.random = cur.random.next;
}
cur = cur.next.next;
}
RandomListNode dummy = new RandomListNode(-1);
RandomListNode curNew = dummy;
cur = head;
while (cur != null) {
curNew.next = cur.next;
curNew = curNew.next;
cur.next = cur.next.next;
cur = cur.next;
}
return dummy.next;
}
public RandomListNode copyRandomList_3(RandomListNode head) {/*StackOverflowError*/
if (head == null) return null;
HashMap<RandomListNode, RandomListNode> map = new HashMap<RandomListNode, RandomListNode>();
return copy(head, map);
}
public RandomListNode copy(RandomListNode root, HashMap<RandomListNode, RandomListNode> map) {
if (root == null) return null;
if (map.containsKey(root) == true) {
return map.get(root);
}
RandomListNode newnode = new RandomListNode(root.label);
map.put(root, newnode);
newnode.next = copy(root.next, map);
newnode.random = copy(root.random, map);
return newnode;
}
public RandomListNode copyRandomList_4(RandomListNode head) {
if (head == null) return null;
HashMap<RandomListNode, RandomListNode> map = new HashMap<RandomListNode, RandomListNode>();
Queue<RandomListNode> queue = new LinkedList<RandomListNode>();
queue.offer(head);
map.put(head, new RandomListNode(head.label));
while (queue.isEmpty() == false) {
RandomListNode cur = queue.poll();
if (cur.next != null && map.containsKey(cur.next) == false) {
RandomListNode newnode = new RandomListNode(cur.next.label);
map.put(cur.next, newnode);
queue.offer(cur.next);
}
map.get(cur).next = map.get(cur.next);
if (cur.random != null && map.containsKey(cur.random) == false) {
RandomListNode newnode = new RandomListNode(cur.random.label);
map.put(cur.random, newnode);
queue.offer(cur.random);
}
map.get(cur).random = map.get(cur.random);
}
return map.get(head);
}
}