21.合并两个有序链表

2024/3/14

# Heading

    21.合并两个有序链表 (opens new window)

    Tags: algorithms amazon apple linkedin microsoft linked-list

    Langs: c cpp csharp golang java javascript kotlin php python python3 ruby rust scala swift typescript

    • algorithms
    • Easy (64.95%)
    • Likes: 1446
    • Dislikes: -
    • Total Accepted: 433.9K
    • Total Submissions: 668.1K
    • Testcase Example: '[1,2,4]\n[1,3,4]'

    将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 

     

    示例:

    输入:1->2->4, 1->3->4
    输出:1->1->2->3->4->4
    
    /*
     * @lc app=leetcode.cn id=21 lang=javascript
     *
     * [21] 合并两个有序链表
     */
    
    // @lc code=start
    /**
     * Definition for singly-linked list.
     * function ListNode(val, next) {
     *     this.val = (val===undefined ? 0 : val)
     *     this.next = (next===undefined ? null : next)
     * }
     */
    /**
     * @param {ListNode} l1
     * @param {ListNode} l2
     * @return {ListNode}
     */
    var mergeTwoLists = function (l1, l2) {
        const hair = new ListNode(0, l1);
        let prev = hair, p;
        while (l1 && l2) {
            if (l2.val < l1.val) {
                p = l2.next;
                l2.next = prev.next;
                prev.next = l2;
                l1 = l2;
                l2 = p;
            } else {
                prev = l1;
                l1 = l1.next;
            }
        }
        l2 && (prev.next = l2);
    
        return hair.next;
    };
    // 递归
    var mergeTwoLists2 = function (l1, l2) {
        if(l1 === null){
            return l2;
        }else if(l2 === null){
            return l1;
        }else if(l1.val <= l2.val){
            l1.next = mergeTwoLists2(l1.next,l2);
            return l1;
        }else{
            l2.next = mergeTwoLists2(l1,l2.next);
            return l2;
        }
    };
    // @lc code=end
    
    
    // @after-stub-for-debug-begin
    module.exports = mergeTwoLists;
    // @after-stub-for-debug-end
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    /*
     * @lc app=leetcode.cn id=21 lang=javascript
     *
     * [21] 合并两个有序链表
     */
    
    // @lc code=start
    /**
     * Definition for singly-linked list.
     * function ListNode(val, next) {
     *     this.val = (val===undefined ? 0 : val)
     *     this.next = (next===undefined ? null : next)
     * }
     */
    var mergeTwoLists = function(l1, l2) {
        const dummy = new ListNode()
        let p = dummy
        while (l1 && l2) {
            if (l1.val < l2.val) {
                p.next = l1
                l1 = l1.next
                p = p.next
            } else {
                p.next = l2
                l2 = l2.next
                p = p.next
            }
        }
    
        if (l1) {
            p.next = l1
        } else if (l2) {
            p.next = l2
        }
    
        return dummy.next
    }
    
    // @lc code=end
    
    // @after-stub-for-debug-begin
    module.exports = mergeTwoLists
    // @after-stub-for-debug-end
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    /*
     * @lc app=leetcode.cn id=21 lang=javascript
     *
     * [21] 合并两个有序链表
     */
    
    // @lc code=start
    /**
     * Definition for singly-linked list.
     * function ListNode(val, next) {
     *     this.val = (val===undefined ? 0 : val)
     *     this.next = (next===undefined ? null : next)
     * }
     */
    var mergeTwoLists = function(l1, l2) {
        if (!l1 && !l2) {
            return null
        } else if (!l1) {
            return l2
        } else if (!l2) {
            return l1
        }
    
        const dummy = new ListNode()
        let p = dummy
        if (l1.val < l2.val) {
            p.next = l1
            l1 = l1.next
        } else {
            p.next = l2
            l2 = l2.next
        }
    
        p.next.next = mergeTwoLists(l1, l2)
        return dummy.next
    }
    
    // @lc code=end
    
    // @after-stub-for-debug-begin
    module.exports = mergeTwoLists
    // @after-stub-for-debug-end
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42