解法1:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
ListNode result = new ListNode(0);
ListNode cur = result;
while(l1 != null && l2 != null) {
if(l1.val <= l2.val) {
cur.next = l1;
l1 = l1.next;
}else {
cur.next = l2;
l2 = l2.next;
}
cur = cur.next;
}
cur.next = l1 == null ? l2 : l1;
return result.next;
思考:如果是合并三个有序链表呢?
class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
class Solution {
public static ListNode mergeTwoLists(ListNode l1, ListNode l2, ListNode l3) {
ListNode result = new ListNode(0);
ListNode cur = result;
while(l1 != null && l2 != null && l3 != null) {
System.out.println("111");
if(l1.val <= l2.val && l1.val <= l3.val) {
cur.next = l1;
l1 = l1.next;
}else if(l2.val <= l1.val && l2.val <= l3.val){
cur.next = l2;
l2 = l2.next;
}else if(l3.val <= l1.val && l3.val <= l2.val) {
cur.next = l3;
l3 = l3.next;
}
cur = cur.next;
}
if(l1 == null) {
while(l2 != null && l3 != null) {
if(l2.val <= l3.val) {
cur.next = l2;
l2 = l2.next;
}else {
cur.next = l3;
l3 = l3.next;
}
cur = cur.next;
}
cur.next = l2 == null ? l3 : l2;
}else if(l2 == null) {
while(l1 != null && l3 != null) {
if(l1.val <= l3.val) {
cur.next = l2;
l1 = l1.next;
}else {
cur.next = l3;
l3 = l3.next;
}
cur = cur.next;
}
cur.next = l1 == null ? l3 : l1;
}else if(l3 == null) {
while(l2 != null && l1 != null) {
if(l2.val <= l1.val) {
cur.next = l2;
l2 = l2.next;
}else {
cur.next = l1;
l1 = l1.next;
}
cur = cur.next;
}
cur.next = l2 == null ? l1 : l2;
}
return result.next;
}
解法2:
递归(感觉大佬的智商高不可攀)
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if(l1 == null) return l2;
if(l2 == null) return l1;
if(l1.val <= l2.val){
l1.next = mergeTwoLists(l1.next, l2);
return l1;
}else{
l2.next = mergeTwoLists(l1, l2.next);
return l2;
}
}
}
三个的比较:
class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
class Solution {
public static ListNode mergeTwoLists(ListNode l1, ListNode l2, ListNode l3) {
if(l1 == null) return fun(l2, l3);
if(l2 == null) return fun(l1, l3);
if(l3 == null) return fun(l1, l2);
if(l1.val <= l2.val && l1.val <= l3.val) {
l1.next = mergeTwoLists(l1.next, l2, l3);
return l1;
}else if(l2.val <= l1.val && l2.val <= l3.val) {
l2.next = mergeTwoLists(l2.next, l1, l3);
return l2;
}else {
l3.next = mergeTwoLists(l3.next, l2, l1);
return l3;
}
}
public static ListNode fun(ListNode l1, ListNode l2) {
if(l1 == null) return l2;
if(l2 == null) return l1;
if(l1.val <= l2.val) {
l1.next = fun(l1.next, l2);
return l1;
}else {
l2.next = fun(l1, l2.next);
return l2;
}
}
public static void main(String args[]) {
ListNode l1 = new ListNode(-1);
ListNode l1_2 = new ListNode(3);
ListNode l2 = new ListNode(0);
ListNode l2_2 = new ListNode(2);
ListNode l3 = new ListNode(1);
ListNode l3_2 = new ListNode(7);
l1.next = l1_2;
l2.next = l2_2;
l3.next = l3_2;
ListNode ll = mergeTwoLists(l1, l2, l3);
while(ll != null) {
System.out.println(ll.val);
ll = ll.next;
}
}
}