Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd80ba79

Browse files
refactor 138
1 parentdffda83 commitd80ba79

File tree

1 file changed

+36
-36
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+36
-36
lines changed

‎src/main/java/com/fishercoder/solutions/_138.java

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,46 +5,46 @@
55

66
/**
77
* 138. Copy List with Random Pointer
8-
*
8+
*
99
* A linked list is given such that each node contains an additional random
1010
* pointer which could point to any node in the list or null.
1111
* Return a deep copy of the list.
12-
* */
12+
*/
1313

1414
publicclass_138 {
15-
publicstaticclassSolution1 {
16-
publicNodecopyRandomList(Nodehead) {
17-
/**Key is the original nodes, value is the new nodes we're deep copying to.*/
18-
Map<Node,Node>map =newHashMap();
19-
Nodenode =head;
20-
21-
//loop for the first time: copy the node themselves with only labels
22-
while (node !=null) {
23-
map.put(node,newNode(node.val));
24-
node =node.next;
25-
}
26-
27-
//loop for the second time: copy random and next pointers
28-
node =head;
29-
while (node !=null) {
30-
map.get(node).next =map.get(node.next);
31-
map.get(node).random =map.get(node.random);
32-
node =node.next;
33-
}
34-
35-
returnmap.get(head);
15+
publicstaticclassSolution1 {
16+
publicNodecopyRandomList(Nodehead) {
17+
/**Key is the original nodes, value is the new nodes we're deep copying to.*/
18+
Map<Node,Node>map =newHashMap();
19+
Nodenode =head;
20+
21+
//loop for the first time: copy the node themselves with only labels
22+
while (node !=null) {
23+
map.put(node,newNode(node.val));
24+
node =node.next;
25+
}
26+
27+
//loop for the second time: copy random and next pointers
28+
node =head;
29+
while (node !=null) {
30+
map.get(node).next =map.get(node.next);
31+
map.get(node).random =map.get(node.random);
32+
node =node.next;
33+
}
34+
35+
returnmap.get(head);
36+
}
37+
38+
// Definition for singly-linked list with a random pointer.
39+
classNode {
40+
intval;
41+
42+
Nodenext;
43+
Noderandom;
44+
45+
Node(intx) {
46+
this.val =x;
47+
}
48+
}
3649
}
37-
38-
// Definition for singly-linked list with a random pointer.
39-
classNode {
40-
intval;
41-
42-
Nodenext;
43-
Noderandom;
44-
45-
Node(intx) {
46-
this.val =x;
47-
}
48-
}
49-
}
5050
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp