LeetCode 21. Merge Two Sorted Lists合并两个有序链表 (C++)

题目:

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.

Example:

Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4

分析:

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

创建一个新的节点head,比较了l1和l2的值的大小,将较小的加入到head,依次比较,如果l1,l2有剩余,就接在后面即可。

l1         l2                              head,p

↓           ↓            ↓

1->2->4      1->3->4                     0

l1         l2                              head p

↓          ↓           ↓   ↓

2->4       1->3->4                     0->1

l1         l2                              head    p

↓          ↓          ↓    ↓

2->4          3->4                          0->1->1

l1         l2                              head        p

↓          ↓          ↓      ↓

4               3->4                          0->1->1->2

l1         l2                              head             p

↓          ↓          ↓        ↓

4                4                              0->1->1->2->3

l1         l2                              head                    p

↓          ↓          ↓              ↓

null            4                               0->1->1->2->3->4

l1         l2                              head                        p

↓          ↓          ↓                   ↓

null            null                           0->1->1->2->3->4->4

最后返回head.next即可。

还可以递归求解此问题。

mergeTwoLists( l1, l2) //l1:1->2->4,l2:1->3->4

=>{1} + mergeTwoLists( l1, l2) //l1:2->4,l2:1->3->4

=>{1->1} + mergeTwoLists( l1, l2) //l1:2->4,l2:3->4

=>{1->1->2} + mergeTwoLists( l1, l2) //l1:4,l2:3->4

=>{1->1->2->3} + mergeTwoLists( l1, l2) //l1:4,l2:4

=>{1->1->2->3->4} + mergeTwoLists( l1, l2) //l1,l2: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) {
        ListNode head(0);
        ListNode* p = &head;
        while(l1 && l2){
            if(l1->val < l2->val){
                p->next = l1;
                l1 = l1->next;
            }
            else{
                p->next = l2;
                l2 = l2->next;
            }
            p = p->next;
        }
        if(l1) p->next = l1;
        if(l2) p->next = l2;
        return head.next;
    }
};
//递归
class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        if(l1 == nullptr) return l2;
        if(l2 == nullptr) return l1;
        if(l1->val < l2->val){
            l1->next = mergeTwoLists(l1->next, l2);
            return l1;
        }
        else{
            l2->next = mergeTwoLists(l1, l2->next);
            return l2;
        }
    }
};

原文地址:https://www.cnblogs.com/silentteller/p/10854539.html

时间: 2024-08-01 07:20:28

LeetCode 21. Merge Two Sorted Lists合并两个有序链表 (C++)的相关文章

[LeetCode]21 Merge Two Sorted Lists 合并两个有序链表

---恢复内容开始--- [LeetCode]21 Merge Two Sorted Lists 合并两个有序链表 Description 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. Example Example: Input: 1->2->4, 1-&g

【LeetCode】21. Merge Two Sorted Lists合并两个有序链表,得到新的有序链表

一.描述: 二.思路: 两个有着相同排序类型(降序或升序)的链表,合并为一新的链表,并且要求有序(和两个子链表的排序相同): 判断2个子链表是否为空,若有空链表,则返回另一不为空的链表: 两者均不为空,判断链表结点值val的大小,(此处应该有2中排序结果,大->小 或 小->大),该题中提交只接受 小->大 类型: 故新的链表头指针指向val值较小的结点,子链表中去除已经被新链表指向的结点,再次重复该操作,即新链表头指针.next指向下一个较小值对应的结点--,即递归实现. 三.代码:

21. Merge Two Sorted Lists合并两个有序链表

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. 不申请新的节点(把两个链表的节点接起来) 提交通过: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { ListNode v(0),*pre = &v,*p

[LeetCode] 23. Merge k Sorted Lists 合并k个有序链表

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 与21. Merge Two Sorted Lists的拓展,这道题要合并k个有序链表,还是可以两两合并. 类似题目: [LeetCode] 21. Merge Two Sorted Lists 合并有序链表 原文地址:https://www.cnblogs.com/lightwindy/p/8512

LeetCode:Merge Two Sorted Lists - 拼接两个有序链表

1.题目名称 Merge Two Sorted Lists(按升序拼接两个有序链表) 2.题目地址 https://leetcode.com/problems/merge-two-sorted-lists/ 3.题目内容 英文: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 l

[LeetCode]80. Merge Two Sorted Lists合并两个排序链表

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. Subscribe to see which companies asked this question 解法1:递归.首先比较头节点大小,若l2->val>l1->val,则返回mergeTwoLists(

Merge Two Sorted Lists, 合并两个有序链表

/**  * 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) {         

Leetcode:Merge Two Sorted Lists 合并两个有序单链表

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. * struct ListNode { * int val; * ListNode *next; * ListNode(int x

leetCode 21. Merge Two Sorted Lists 合并链表

21. Merge Two Sorted Lists 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-lin