Skip to content

Commit c177744

Browse files
authored
Merge pull request #2125 from hjeomdev/main
[hjeomdev] WEEK 04 solutions
2 parents 75ace28 + 9d7c188 commit c177744

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
ListNode list = new ListNode(-1);
14+
ListNode result = list;
15+
while (list1 != null && list2 != null) {
16+
if (list1.val < list2.val) {
17+
result.next = list1;
18+
list1 = list1.next;
19+
} else {
20+
result.next = list2;
21+
list2 = list2.next;
22+
}
23+
result = result.next;
24+
}
25+
result.next = list1 != null ? list1 : list2;
26+
return list.next;
27+
}
28+
}

0 commit comments

Comments
 (0)