-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListNode.java
More file actions
52 lines (46 loc) · 1.17 KB
/
ListNode.java
File metadata and controls
52 lines (46 loc) · 1.17 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
44
45
46
47
48
49
50
51
52
import java.util.ArrayList;
import java.util.List;
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 numToList(int[] nums) {
ListNode dummy = new ListNode(-1);
ListNode curr = dummy;
for(int n: nums) {
curr.next = new ListNode(n);
curr = curr.next;
}
return dummy.next;
}
public static List<Integer> listToArrayList(ListNode head) {
List<Integer> arr = new ArrayList<Integer>();
while (head!=null) {
arr.add(head.val);
head = head.next;
}
return arr;
}
public static ListNode merge(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(-1);
ListNode curr = dummy;
while (l1!=null) {
curr.next = l1;
curr = curr.next;
l1 = l1.next;
}
while (l2!=null) {
curr.next = l2;
curr = curr.next;
l2 = l2.next;
}
return dummy.next;
}
}