leetcode 21题---合并两个有序链表

在这里插入图片描述

解法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;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值