力扣题——合并两个有序链表

本文介绍了一种算法,用于将两个已排序的链表合并成一个新的有序链表。通过比较两个链表的节点值,该算法有效地实现了链表的合并,确保了结果链表的有序性。

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

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

示例:

输入:1->2->4, 1->3->4
输出:1->1->2->3->4->4

/**
 * 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* head=NULL;
        if(l1->val < l2->val){
            head=l1;
            l1=l1->next;
        }
        else{
            head=l2;
            l2=l2->next;
        }
        ListNode* tail=head;
        while(l1 && l2){
            if(l1->val < l2->val){
                tail->next=l1;
                l1=l1->next;
            }
            else{
                tail->next=l2;
                l2=l2->next;
            }
            tail=tail->next;
        }
        if(l1)
            tail->next=l1;
        else
            tail->next=l2;
            
            
        return head;  */
        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;

        }

        
    }
};
### 力扣第21合并两个有序链表 以下是力扣第21的完整代码解决方案。该问的目标是将两个升序链表合并为一个新的升序链表。 #### Python 实现 ```python # 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, l1: ListNode, l2: ListNode) -> ListNode: # 创建一个虚拟节点作为新链表的起点 dummy = ListNode(-1) current = dummy # 遍历两个链表直到其中一个为空 while l1 and l2: if l1.val < l2.val: current.next = l1 l1 = l1.next else: current.next = l2 l2 = l2.next current = current.next # 如果l1还有剩余,则直接连接到当前链表后面 if l1 is not None: current.next = l1 # 如果l2还有剩余,则直接连接到当前链表后面 if l2 is not None: current.next = l2 # 返回新的链表头部(跳过虚拟节点) return dummy.next ``` 此实现通过创建一个虚拟节点 `dummy` 来简化边界条件处理[^5]。算法的时间复杂度为 O(n+m),其中 n 和 m 是两个链表的长度,空间复杂度为 O(1)。 --- #### C++ 实现 ```cpp // 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* l1, ListNode* l2) { // 创建一个虚拟节点作为新链表的起点 ListNode dummy(0); ListNode* current = &dummy; // 遍历两个链表直到其中一个为空 while (l1 && l2) { if (l1->val < l2->val) { current->next = l1; l1 = l1->next; } else { current->next = l2; l2 = l2->next; } current = current->next; } // 连接剩余部分 if (l1 != nullptr) { current->next = l1; } else if (l2 != nullptr){ current->next = l2; } // 返回新的链表头部(跳过虚拟节点) return dummy.next; } }; ``` 在此 C++ 版本中,同样采用了虚拟节点来管理指针操作,从而减少额外的逻辑分支[^6]。 --- #### Java 实现 ```java // Definition for singly-linked list. 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 dummy = new ListNode(-1); ListNode current = dummy; // 遍历两个链表直到其中一个为空 while (l1 != null && l2 != null) { if (l1.val < l2.val) { current.next = l1; l1 = l1.next; } else { current.next = l2; l2 = l2.next; } current = current.next; } // 连接剩余部分 if (l1 != null) { current.next = l1; } else if (l2 != null) { current.next = l2; } // 返回新的链表头部(跳过虚拟节点) return dummy.next; } } ``` Java 的实现方式与上述两种语言一致,均利用了虚拟节点技术以优化代码结构并提高可读性[^7]。 --- ### 总结 以上提供了三种主流编程语言(Python、C++ 和 Java)针对 LeetCode 第 21 的完整代码实现方案。每种语言都采用了一致的核心思路——借助虚拟节点完成两链表的合并过程,并最终返回结果链表的起始位置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值