-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListNode.java
More file actions
37 lines (30 loc) · 894 Bytes
/
Copy pathListNode.java
File metadata and controls
37 lines (30 loc) · 894 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
package datastructs;
import java.util.ArrayList;
import java.util.List;
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; }
public ListNode(List<Integer> numbers) {
var rst = new ListNode();
var head = rst;
for (Integer number : numbers) {
rst.next = new ListNode(val);
rst = rst.next;
rst.val = number;
}
this.val = head.next.val;
this.next = head.next.next;
}
public String printAsList(){
var rst = new ArrayList<String>();
var cur = this;
while (cur != null){
rst.add(String.valueOf(cur.val));
cur = cur.next;
}
return String.join(", ", rst);
}
}