1public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
2 ListNode dummy = new ListNode(0);
3 ListNode current = dummy;
4
5 ListNode p = list1;
6 ListNode q = list2;
7
8 while (p != null && q != null) {
9 if (p.val <= q.val) {
10 current.next = p;
11 p = p.next;
12 } else {
13 current.next = q;
14 q = q.next;
15 }
16 current = current.next;
17 }
18
19 if (p != null) {
20 current.next = p;
21 } else {
22 current.next = q;
23 }
24
25 return dummy.next;
26}