Skip to content

Commit b222c9a

Browse files
committed
add toString method in ListNode
1 parent 149442b commit b222c9a

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/com/blankj/structure/ListNode.java

+15
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,19 @@ public static void print(ListNode listNode) {
5050
}
5151
System.out.println(str.append("]"));
5252
}
53+
54+
public String toString(ListNode listNode) {
55+
if (listNode == null) {
56+
System.out.println("null");
57+
return "";
58+
}
59+
StringBuilder str = new StringBuilder("[" + String.valueOf(listNode.val));
60+
ListNode p = listNode.next;
61+
while (p != null) {
62+
str.append(",").append(String.valueOf(p.val));
63+
p = p.next;
64+
}
65+
str.append("]");
66+
return str.toString();
67+
}
5368
}

0 commit comments

Comments
 (0)