Title description
Input two monotonically increasing linked lists, and output the synthesized linked list of the two linked lists. Of course, we need the synthesized linked list to satisfy the monotonic non-decreasing rule.
/*
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;
}
}