-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathListNode.java
More file actions
42 lines (37 loc) · 1001 Bytes
/
ListNode.java
File metadata and controls
42 lines (37 loc) · 1001 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
37
38
39
40
41
42
package Common;
import java.util.List;
// Definition for singly-linked list.
public class ListNode {
public int val;
public ListNode next;
public ListNode(final int x) {
val = x;
next = null;
}
public ListNode(List<Integer> array) {
if (array.size() <= 0) {
return;
}
val = array.get(0);
ListNode tmp = null;
for (int i = 1; i < array.size(); i++) {
if (tmp == null) {
tmp = new ListNode(array.get(i));
next = tmp;
} else {
ListNode node = new ListNode(array.get(i));
tmp.next = node;
tmp = node;
}
}
}
public void printListNode() {
System.out.println();
ListNode tmp = this;
while (tmp != null) {
System.out.print(tmp.val + ((tmp.next != null) ? "," : "\n"));
tmp = tmp.next;
}
System.out.println();
}
}