Skip to content

Improved Node.toString() #868

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 0 additions & 27 deletions src/main/kotlin/com_github_leetcode/neighbors/Node.kt

This file was deleted.

37 changes: 20 additions & 17 deletions src/main/kotlin/com_github_leetcode/random/Node.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com_github_leetcode.random

import java.util.StringJoiner

class Node {
var `val`: Int
var next: Node? = null
Expand All @@ -12,33 +10,38 @@ class Node {
}

override fun toString(): String {
val result = StringJoiner(",", "[", "]")
val result2 = StringJoiner(",", "[", "]")
result2.add(`val`.toString())
val result = StringBuilder()
result.append("[")
result.append("[")
result.append(`val`)
result.append(",")
if (random == null) {
result2.add("null")
result.append("null")
} else {
result2.add(random!!.`val`.toString())
result.append(random!!.`val`)
}
result.add(result2.toString())
result.append("]")
var curr = next
while (curr != null) {
val result3 = StringJoiner(",", "[", "]")
result3.add(curr.`val`.toString())
result.append(",")
result.append("[")
result.append(curr.`val`)
result.append(",")
if (curr.random == null) {
result3.add("null")
result.append("null")
} else {
var randomIndex = 0
var curr2: Node? = this
while (curr2!!.next != null && curr2 !== curr.random) {
randomIndex += 1
curr2 = curr2.next
var indexFinder: Node? = this
while (indexFinder!!.next != null && indexFinder !== curr.random) {
randomIndex++
indexFinder = indexFinder.next
}
result3.add(randomIndex.toString())
result.append(randomIndex)
}
result.add(result3.toString())
result.append("]")
curr = curr.next
}
result.append("]")
return result.toString()
}
}
9 changes: 4 additions & 5 deletions src/main/kotlin/g0101_0200/s0133_clone_graph/Solution.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package g0101_0200.s0133_clone_graph

// #Medium #Hash_Table #Depth_First_Search #Breadth_First_Search #Graph #Udemy_Graph
// #Top_Interview_150_Graph_General #2022_10_09_Time_351_ms_(60.91%)_Space_37.1_MB_(70.56%)
// #Top_Interview_150_Graph_General #2025_08_03_Time_133_ms_(88.96%)_Space_43.11_MB_(67.94%)

import com_github_leetcode.neighbors.Node
import com_github_leetcode.Node

/*
* Definition for a Node.
Expand All @@ -24,11 +24,10 @@ class Solution {
}
val newNode = Node(node.`val`)
processedNodes[node] = newNode
newNode.neighbors = ArrayList()
for (neighbor in node.neighbors) {
val clonedNeighbor: Node? = cloneGraph(neighbor, processedNodes)
val clonedNeighbor = cloneGraph(neighbor, processedNodes)
if (clonedNeighbor != null) {
newNode.neighbors.add(clonedNeighbor)
(newNode.neighbors as ArrayList).add(clonedNeighbor)
}
}
return newNode
Expand Down
7 changes: 0 additions & 7 deletions src/test/kotlin/com_github_leetcode/NodeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@ import org.hamcrest.MatcherAssert.assertThat
import org.junit.jupiter.api.Test

internal class NodeTest {
@Test
fun constructor() {
val node = Node()
assertThat(node.`val`, equalTo(0))
assertThat(node.toString(), equalTo("[]"))
}

@Test
fun constructor2() {
val node = Node(1)
Expand Down
30 changes: 0 additions & 30 deletions src/test/kotlin/com_github_leetcode/neighbors/NodeTest.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package g0101_0200.s0133_clone_graph

import com_github_leetcode.neighbors.Node
import com_github_leetcode.Node
import org.hamcrest.CoreMatchers.equalTo
import org.hamcrest.MatcherAssert.assertThat
import org.junit.jupiter.api.Test
Expand Down
Loading