21. Merge Two Sorted Lists

本文介绍了一种方法来合并两个已排序的链表,并创建一个新的排序链表。该方法通过遍历两个输入链表,比较节点值并选择较小者添加到结果链表中,直至其中一个链表耗尽。最后将剩余的链表连接到结果链表末尾。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
         
        if(l1==null)
            return l2;
        if(l2==null)
            return l1;
         
        ListNode dump = new ListNode(-1);
        ListNode cursor = dump;
        while(l1!=null&&l2!=null){
            if(l1.val<l2.val){
                cursor.next=l1;
                l1=l1.next;
                cursor=cursor.next;
            }else{
                cursor.next=l2;
                l2=l2.next;
                cursor=cursor.next;
            }
        }
         
        if(l1==null)
            cursor.next=l2;
        else
            cursor.next=l1;
         
        return dump.next;
    }
}
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if (l1 == null)
            return l2;
        else if (l2 == null)
            return l1;
        
        if (l1.val > l2.val) {
            l2.next = mergeTwoLists(l1,l2.next);
            return l2;
        }
        
        l1.next = mergeTwoLists(l1.next,l2);
        return l1;
    }
}

JS

/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} l1
 * @param {ListNode} l2
 * @return {ListNode}
 */
var mergeTwoLists = function(l1, l2) {
    let l3 = new ListNode(-1),
    p = l3;
    while(l1 && l2) {
        if (l1.val <= l2.val) {
            p.next = new ListNode(l1.val);
            l1 = l1.next;
        } else {
            p.next = new ListNode(l2.val);
            l2 = l2.next;
        }
        p = p.next;
    }
    p.next = l1 ? l1 : l2;
    return l3.next;    
};
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if (l1 == null) return l2;
        if (l2 == null) return l1;
        
        ListNode result = new ListNode(0);
        ListNode curr = result;
        
        // add smallest from both lists
        while (l1 != null) {    
               
			// if l2 is exhausted or l1 is smaller: add l1
            if (l2 == null || l1.val <= l2.val) {
                curr.next = l1;
                curr = curr.next;
                l1 = l1.next;
            } else { // add l2
                curr.next = l2;
                curr = curr.next;
                l2 = l2.next;
            }
        }
        
        // finish rest of l2 if not exhausted
        while (l2 != null) {
            curr.next = l2;
            curr = curr.next;
            l2 = l2.next;
        }
        
        return result.next;
    }
}
/**
 * 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) {
        ListNode ans= new ListNode(0);
        ListNode ansTemp = ans;
        ListNode temp1 = l1;
        ListNode temp2 = l2;
        
        while(temp1!=null || temp2!=null){
            if(temp2 == null || temp1 != null && temp1.val<=temp2.val){
                ansTemp.next = temp1;
                temp1 = temp1.next;
            }
            else if(temp1==null || temp2!=null && temp2.val < temp1.val){
                ansTemp.next = temp2;
                temp2 = temp2.next;
            }
            ansTemp = ansTemp.next;
        }
        return ans.next;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值