Skip to content

Commit 818402e

Browse files
committed
add: merge two sorted lists solution
1 parent dd41204 commit 818402e

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9+
* }
10+
*/
11+
class Solution {
12+
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
13+
if (list1 == null || list2 == null) {
14+
return list1 == null ? list2 : list1;
15+
}
16+
if (list1.val < list2.val) {
17+
list1.next = mergeTwoLists(list1.next, list2);
18+
return list1;
19+
} else {
20+
list2.next = mergeTwoLists(list2.next, list1);
21+
return list2;
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)