Skip to content

Commit 5b11a4c

Browse files
committed
feat: merge-two-sorted-lists solution
1 parent 54bde43 commit 5b11a4c

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

merge-two-sorted-lists/sm9171.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
3+
ListNode dummy = new ListNode(-1);
4+
ListNode node = dummy;
5+
6+
while (list1 != null && list2 != null) {
7+
if (list1.val < list2.val) {
8+
node.next = list1;
9+
list1 = list1.next;
10+
} else {
11+
node.next = list2;
12+
list2 = list2.next;
13+
}
14+
node = node.next;
15+
}
16+
17+
node.next = list1 != null ? list1 : list2;
18+
return dummy.next;
19+
}
20+
}

0 commit comments

Comments
 (0)