-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
31 lines (30 loc) · 847 Bytes
/
Solution.java
File metadata and controls
31 lines (30 loc) · 847 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
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode res = null,
head = null;
while (l1 != null && l2 != null) {
if (res == null) {
head = res = new ListNode(Math.min(l1.val, l2.val));
} else {
head.next = new ListNode(Math.min(l1.val, l2.val));
head = head.next;
}
if (l1.val <= l2.val) {
l1 = l1.next;
} else {
l2 = l2.next;
}
}
if (res == null) res = l1 != null ? l1 : l2;
else head.next = l1 != null ? l1 : l2;
return res;
}
}