题目描述
输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。
/**
* 合并了两个排序的链表
*
* @param list1
* @param list2
* @return
*/
public ListNode Merge(ListNode list1, ListNode list2) {
ListNode start = null;
ListNode current = null;
if (list1 == null && list2 == null) {
return null;
} else if (list1 == null) {
return list2;
} else if (list2 == null) {
return list1;
}
while (true) {
if (start == null) {
if (list1.val < list2.val) {
start = list1;
list1 = list1.next;
} else {
start = list2;
list2 = list2.next;
}
current = start;
continue;
}
if (list1 == null) {
current.next = list2;
break;
}
if (list2 == null) {
current.next = list1;
break;
}
if (list1.val < list2.val) {
current.next = list1;
list1 = list1.next;
} else {
current.next = list2;
list2 = list2.next;
}
current = current.next;
}
return start;
}