forked from javadev/LeetCode-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListNode.java
More file actions
36 lines (30 loc) · 833 Bytes
/
ListNode.java
File metadata and controls
36 lines (30 loc) · 833 Bytes
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
package com_github_leetcode;
import java.util.Objects;
@SuppressWarnings("java:S1104")
public class ListNode {
public int val;
public ListNode next;
public ListNode() {}
public ListNode(int val) {
this.val = val;
}
public ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
@Override
public String toString() {
StringBuilder result = new StringBuilder("" + val);
if (Objects.nonNull(next)) {
ListNode current = next;
while (current.next != null) {
result.append(", ");
result.append(current.val);
current = current.next;
}
result.append(", ");
result.append(current.val);
}
return result.toString();
}
}