-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListNode.java
More file actions
43 lines (37 loc) · 1.08 KB
/
ListNode.java
File metadata and controls
43 lines (37 loc) · 1.08 KB
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
37
38
39
40
41
42
43
public class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
public static ListNode createList(int[] vals) {
if (vals == null || vals.length == 0)
return null;
ListNode head = new ListNode();
ListNode current = head;
for (int i = 0; i < vals.length; i++) {
current.val = vals[i];
if (i != vals.length-1)
current.next = new ListNode();
current = current.next;
}
return head;
}
public String toString() {
String s = "";
ListNode current = this;
while (current != null) {
s = s + current.val;
current = current.next;
if (current != null)
s = s + ", ";
}
return "["+ s +"]";
}
public void print() {
System.out.println(this.toString());
}
public static void main(String[] args) {
System.out.println(" ");
}
}