本问题对应的 leetcode 原文链接:剑指 Offer 25. 合并两个排序的链表
问题描述
输入两个递增排序的链表,合并这两个链表并使新链表中的节点仍然是递增排序的。
示例1:
输入:1->2->4, 1->3->4
输出:1->1->2->3->4->4
限制:
0 <= 链表长度 <= 1000
解题思路
视频讲解直达: 本题视频讲解
代码实现
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode merge = new ListNode(0);
ListNode temp = merge;
while(l1 != null && l2 != null){
if(l1.val < l2.val){
temp.next = l1;
l1 = l1.next;
} else {
temp.next = l2;
l2 = l2.next;
}
temp = temp.next;
}
temp.next = l1 == null ? l2 : l1;
return merge.next;
}
}
时间复杂度:O(n+m),n 和 m 表示两个链表长度
空间复杂度:O(1)
Python
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def mergeTwoLists(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
merge = ListNode(0)
temp = merge
while l1 and l2:
if l1.val < l2.val:
temp.next = l1
l1 = l1.next
else:
temp.next = l2
l2 = l2.next
temp = temp.next
temp.next = l1 if l1 else l2
return merge.next
C++
/**
* 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) {
ListNode* merge = new ListNode(0);
ListNode* temp = merge;
while (l1 && l2) {
if (l1->val < l2->val) {
temp->next = l1;
l1 = l1->next;
} else {
temp->next = l2;
l2 = l2->next;
}
temp = temp->next;
}
temp->next = l1 ? l1 : l2;
return merge->next;
}
};
Go
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
merge := &ListNode{}
temp := merge
for l1 != nil && l2 != nil {
if l1.Val < l2.Val {
temp.Next = l1
l1 = l1.Next
} else {
temp.Next = l2
l2 = l2.Next
}
temp = temp.Next
}
if l1 != nil {
temp.Next = l1
} else {
temp.Next = l2
}
return merge.Next
}
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 merge = new ListNode(0);
let temp = merge;
while (l1 != null && l2 != null) {
if (l1.val < l2.val) {
temp.next = l1;
l1 = l1.next;
} else {
temp.next = l2;
l2 = l2.next;
}
temp = temp.next;
}
temp.next = l1 == null ? l2 : l1;
return merge.next;
};
评论(11)
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode preNode = null;
ListNode headNode = null;
ListNode curNode = null;
while(l1 != null || l2 != null){
if(l1 null){
if(preNode null){
headNode = l2;
}else {
preNode.next = l2;
}
return headNode;
}
else if(l2 null){
if(preNode null){
headNode = l1;
}else {
preNode.next = l1;
}
return headNode;
}
else {
if (l1.val <= l2.val) {
curNode = l1;
l1 = l1.next;
} else {
curNode = l2;
l2 = l2.next;
}
if(preNode null){
headNode = curNode;
preNode = curNode;
}
preNode.next = curNode;
preNode = curNode;
}
}
return headNode;
}
}
时间复杂度o(1),o(n)
定义一个虚拟头结点,也就是 dummy 节点,它相当于是个占位符,可以避免处理空指针的情况,降低代码的复杂性。
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
// 参考归并排序,时间复杂度为O(m+n)
ListNode *p1 = l1, *p2 = l2;
ListNode *ans = new ListNode();
};
时间复杂度O(n+m)
空间复杂度O(1)
时间复杂度 O(n+m) 分别是两条链表的长度,也可以理解为O(n)
空间复杂度 O(1)
C++ 题解
额