-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.java
More file actions
37 lines (30 loc) · 751 Bytes
/
Node.java
File metadata and controls
37 lines (30 loc) · 751 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
import java.util.LinkedList;
public class Node {
public int val;
public Node next;
public Node() {}
public Node(int _val) {
val = _val;
}
public Node(int _val, Node _next) {
val = _val;
next = _next;
}
public static Node numsToNode(int[] nums) {
Node dummy = new Node(-1);
Node curr = dummy;
for(int n: nums) {
curr.next = new Node(n);
curr = curr.next;
}
return dummy.next;
}
public static LinkedList nodeToLinkedList(Node head) {
LinkedList<Integer> res = new LinkedList<>();
while ((head!=null)) {
res.add(head.val);
head = head.next;
}
return res;
}
}