タイトル説明
単調に増加する2つのリンクリストを入力し、2つのリンクリストの合成リンクリストを出力します。
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode Merge(ListNode list1,ListNode list2) {
ListNode head = new ListNode(0);
ListNode mergelist = head;
while (list1 != null && list2 != null){
if (list1.val > list2.val) {
mergelist.next = list2;
list2 = list2.next;
}else {
mergelist.next = list1;
list1 = list1.next;
}
mergelist = mergelist.next;
}
if (list1 == null) mergelist.next = list2;
if (list2 == null) mergelist.next = list1;
return head.next;
}
}