Skip to content

Commit ec35883

Browse files
authored
Improved task 138
1 parent a00b92f commit ec35883

File tree

4 files changed

+37
-62
lines changed

4 files changed

+37
-62
lines changed

src/main/kotlin/com_github_leetcode/random/Node.kt

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,10 @@ class Node {
77
var next: Node? = null
88
var random: Node? = null
99

10-
constructor() {
11-
`val` = 0
12-
}
13-
1410
constructor(`val`: Int) {
1511
this.`val` = `val`
1612
}
1713

18-
constructor(`val`: Int, next: Node?, random: Node?) {
19-
this.`val` = `val`
20-
this.next = next
21-
this.random = random
22-
}
23-
2414
override fun toString(): String {
2515
val result = StringJoiner(",", "[", "]")
2616
val result2 = StringJoiner(",", "[", "]")

src/main/kotlin/g0101_0200/s0138_copy_list_with_random_pointer/Solution.kt

Lines changed: 13 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package g0101_0200.s0138_copy_list_with_random_pointer
22

33
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Hash_Table #Linked_List
44
// #Programming_Skills_II_Day_14 #Udemy_Linked_List #Top_Interview_150_Linked_List
5-
// #Big_O_Time_O(N)_Space_O(N) #2022_09_03_Time_274_ms_(80.58%)_Space_40.5_MB_(58.99%)
5+
// #Big_O_Time_O(N)_Space_O(N) #2025_07_04_Time_123_ms_(90.70%)_Space_43.99_MB_(97.67%)
66

77
import com_github_leetcode.random.Node
88

@@ -18,48 +18,19 @@ import com_github_leetcode.random.Node
1818
*/
1919
class Solution {
2020
fun copyRandomList(head: Node?): Node? {
21-
if (head == null) {
22-
return null
21+
val hashMap: MutableMap<Node?, Node> = HashMap()
22+
var cur = head
23+
while (cur != null) {
24+
hashMap.put(cur, Node(cur.`val`))
25+
cur = cur.next
2326
}
24-
// first pass to have a clone node point to the next node. ie A->B becomes A->clonedNode->B
25-
var curr: Node? = head
26-
while (curr != null) {
27-
val clonedNode = Node(curr.`val`)
28-
clonedNode.next = curr.next
29-
curr.next = clonedNode
30-
curr = clonedNode.next
27+
cur = head
28+
while (cur != null) {
29+
val copy: Node = hashMap[cur]!!
30+
copy.next = hashMap[cur.next]
31+
copy.random = hashMap[cur.random]
32+
cur = cur.next
3133
}
32-
curr = head
33-
// second pass to make the cloned node's random pointer point to the orginal node's randome
34-
// pointer.
35-
// ie. A's random pointer becomes ClonedNode's random pointer
36-
while (curr != null) {
37-
if (curr.random != null) {
38-
curr.next?.random = curr.random!!.next
39-
} else {
40-
curr.next?.random = null
41-
}
42-
curr = curr.next?.next
43-
}
44-
curr = head
45-
// third pass to restore the links and return the head of the cloned nodes' list.
46-
var newHead: Node? = null
47-
while (curr != null) {
48-
var clonedNode: Node
49-
if (newHead == null) {
50-
clonedNode = curr.next!!
51-
newHead = clonedNode
52-
} else {
53-
clonedNode = curr.next!!
54-
}
55-
curr.next = clonedNode.next
56-
if (curr.next != null) {
57-
clonedNode.next = curr.next!!.next
58-
} else {
59-
clonedNode.next = null
60-
}
61-
curr = curr.next
62-
}
63-
return newHead
34+
return hashMap[head]
6435
}
6536
}

src/test/kotlin/com_github_leetcode/random/NodeTest.kt

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import org.junit.jupiter.api.Test
77
internal class NodeTest {
88
@Test
99
fun constructor() {
10-
val node = Node()
10+
val node = Node(0)
1111
assertThat(node.`val`, equalTo(0))
1212
assertThat(node.toString(), equalTo("[[0,null]]"))
1313
}
@@ -21,18 +21,23 @@ internal class NodeTest {
2121

2222
@Test
2323
fun constructor3() {
24-
val node = Node(1, Node(2), Node(3))
24+
val node = Node(1)
25+
node.next = Node(2)
26+
node.random = Node(3)
2527
assertThat(node.`val`, equalTo(1))
2628
assertThat(node.toString(), equalTo("[[1,3],[2,null]]"))
2729
}
2830

2931
@Test
3032
fun constructor4() {
31-
val node = Node(
32-
1,
33-
Node(2, Node(21), Node(22)),
34-
Node(3, null, Node(32)),
35-
)
33+
val next = Node(2)
34+
next.next = Node(21)
35+
next.random = Node(22)
36+
val random = Node(3)
37+
random.random = Node(32)
38+
val node = Node(1)
39+
node.next = next
40+
node.random = random
3641
assertThat(node.`val`, equalTo(1))
3742
assertThat(node.toString(), equalTo("[[1,3],[2,2],[21,null]]"))
3843
}

src/test/kotlin/g0101_0200/s0138_copy_list_with_random_pointer/SolutionTest.kt

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ internal class SolutionTest {
2323
node11.random = node1
2424
node10.random = node11
2525
node1.random = node7
26-
assertThat(Solution().copyRandomList(node7).toString(), equalTo("[[7,null],[13,0],[11,4],[10,2],[1,0]]"))
26+
assertThat(
27+
Solution().copyRandomList(node7).toString(),
28+
equalTo("[[7,null],[13,0],[11,4],[10,2],[1,0]]"),
29+
)
2730
}
2831

2932
@Test
@@ -34,7 +37,10 @@ internal class SolutionTest {
3437
node1.random = node1
3538
node2.next = null
3639
node2.random = node2
37-
assertThat(Solution().copyRandomList(node1).toString(), equalTo("[[1,1],[2,1]]"))
40+
assertThat(
41+
Solution().copyRandomList(node1).toString(),
42+
equalTo("[[1,1],[2,1]]"),
43+
)
3844
}
3945

4046
@Test
@@ -48,6 +54,9 @@ internal class SolutionTest {
4854
node32.random = node31
4955
node33.next = null
5056
node33.random = null
51-
assertThat(Solution().copyRandomList(node31).toString(), equalTo("[[3,null],[3,0],[3,null]]"))
57+
assertThat(
58+
Solution().copyRandomList(node31).toString(),
59+
equalTo("[[3,null],[3,0],[3,null]]"),
60+
)
5261
}
5362
}

0 commit comments

Comments
 (0)