列表

详情


21. 合并两个有序链表

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

 

示例 1:

输入:l1 = [1,2,4], l2 = [1,3,4]
输出:[1,1,2,3,4,4]

示例 2:

输入:l1 = [], l2 = []
输出:[]

示例 3:

输入:l1 = [], l2 = [0]
输出:[0]

 

提示:

相似题目

合并K个升序链表

合并两个有序数组

排序链表

最短单词距离 II

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} * ListNode(int x, ListNode *next) : val(x), next(next) {} * }; */ class Solution { public: ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) { } };

python3 解法, 执行用时: 40 ms, 内存消耗: 16.2 MB, 提交时间: 2023-08-05 23:24:45

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
        if list1 == None:
            return list2
        if list2 == None:
            return list1

        list3 = None
        if list1.val < list2.val:
            list3 = list1
            list3.next = self.mergeTwoLists(list1.next, list2)
        else:
            list3 = list2
            list3.next = self.mergeTwoLists(list1, list2.next)
        
        return list3

golang 解法, 执行用时: 0 ms, 内存消耗: 2.5 MB, 提交时间: 2021-07-21 10:48:46

/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
	if l1 == nil {
		return l2
	}
	if l2 == nil {
		return l1
	}
	var l3 *ListNode
	if l1.Val < l2.Val {
		l3 = l1
		l3.Next = mergeTwoLists(l1.Next, l2)
	} else {
		l3 = l2
		l3.Next = mergeTwoLists(l1, l2.Next)
	}
	return l3
}

php 解法, 执行用时: 16 ms, 内存消耗: 15.5 MB, 提交时间: 2021-05-28 14:46:01

/**
 * Definition for a singly-linked list.
 * class ListNode {
 *     public $val = 0;
 *     public $next = null;
 *     function __construct($val = 0, $next = null) {
 *         $this->val = $val;
 *         $this->next = $next;
 *     }
 * }
 */
class Solution {

    /**
     * @param ListNode $l1
     * @param ListNode $l2
     * @return ListNode
     */
    function mergeTwoLists($node1, $node2) {
        $prehead = new ListNode(-1);
        $prev = $prehead;

        while ( $node1 != null && $node2 != null ) {
            if ( $node1->val <= $node2->val ) {
                $prev->next = $node1;
                $node1 = $node1->next;
            } else {
                $prev->next = $node2;
                $node2 = $node2->next;
            }
            $prev = $prev->next;
        }
        $prev->next = $node1 == null ? $node2 : $node1;

        return $prehead->next;
    }
}

javascript 解法, 执行用时: 100 ms, 内存消耗: N/A, 提交时间: 2018-08-29 13:26:30

/**
 * 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) {
    if ( l1 == null ) return l2;
    if ( l2 == null ) return l1;
    var l3 = ListNode(0);
    if ( l1.val < l2.val ) {
        l3 = l1;
        l3.next = mergeTwoLists(l1.next, l2);
    } else {
        l3 = l2;
        l3.next = mergeTwoLists(l1, l2.next);
    }
    return l3;
};

cpp 解法, 执行用时: 12 ms, 内存消耗: N/A, 提交时间: 2018-08-29 13:24:47

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        if ( l1 == NULL ) return l2;
        if ( l2 == NULL ) return l1;
        ListNode* l3 = NULL;
        if ( l1->val < l2->val ) {
            l3 = l1;
            l3->next = mergeTwoLists(l1->next, l2);
        } else {
            l3 = l2;
            l3->next = mergeTwoLists(l1, l2->next);
        }
        return l3;
    }
};

golang 解法, 执行用时: 8 ms, 内存消耗: N/A, 提交时间: 2018-08-29 13:20:51

/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
    if l1 == nil {
        return l2
    }
    if l2 == nil {
        return l1
    }
    var l3 *ListNode
    if l1.Val < l2.Val {
        l3 = l1
        l3.Next = mergeTwoLists(l1.Next, l2)
    } else {
        l3 = l2
        l3.Next = mergeTwoLists(l1, l2.Next)
    }
    return l3
}

java 解法, 执行用时: 11 ms, 内存消耗: N/A, 提交时间: 2018-08-29 13:17:26

/**
 * 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 l3 = new ListNode(0);
        if ( l1.val < l2.val ) {
            l3 = l1;
            l3.next = mergeTwoLists(l1.next, l2);
        } else {
            l3 = l2;
            l3.next = mergeTwoLists(l1, l2.next);
        }
        return l3;
    }
}

python3 解法, 执行用时: 84 ms, 内存消耗: N/A, 提交时间: 2018-08-29 12:53:32

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def mergeTwoLists(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        if l1 == None:
            return l2
        if l2 == None:
            return l1
        l3 = ListNode(0)
        if l1.val < l2.val:
            l3 = l1
            l3.next = self.mergeTwoLists(l1.next, l2)
        else:
            l3 = l2
            l3.next = self.mergeTwoLists(l1, l2.next)
        return l3

上一题