一天一道LeetCode系列

(一)题目

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.

(二)解题

这题是剑指offer上的老题了,剑指上面用的是递归,我写了个非递归的版本。

/**
 * 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 = NULL;
        if(l1==NULL && l2==NULL) return head;
        ListNode* p = NULL;
        ListNode* p1 = l1;
        ListNode* p2 = l2;
        while(p1 != NULL || p2!=NULL)
        {
            if(p1 != NULL && p2 != NULL)
            {
                if(p1->val < p2->val)
                {
                    if(head == NULL) //记录头节点head
                    {
                        head = p1;
                        p = head;
                    }
                    else {p->next = p1;p=p->next;}
                    p1=p1->next;

                }
                else
                {
                    if(head == NULL)
                    {
                        head = p2;
                        p = head;
                    }
                    else {p->next = p2;p=p->next;}
                    p2=p2->next;
                }
            }
            if(p1 == NULL && p2 != NULL)
            {
                if(head == NULL)
                {
                    head = p2;
                    p = head;
                }
                else {p->next = p2;p=p->next;}
                p2=p2->next;
            }
            if(p1 != NULL && p2 == NULL)
            {
                if(head == NULL)
                {
                    head = p1;
                    p = head;
                }
                else {p->next = p1;p=p->next;}
                p1=p1->next;
            }
        }
        return head;
    }
};

递归版本:

class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        if(l1 == NULL) return l2;
        if(l2 == NULL) return l1;
        ListNode* head;
        if(l1->val < l2->val)
        {
            head = l1;
            head->next = mergeTwoLists(l1->next , l2);
        }
        else
        {
            head = l2;
            head->next = mergeTwoLists(l1 , l2->next);
        }
        return head;
    }
};

【一天一道LeetCode】#21. Merge Two Sorted Lists的更多相关文章

  1. [LeetCode] 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 t ...

  2. [LeetCode] 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 t ...

  3. [leetcode] 21. Merge Two Sorted Lists (Easy)

    合并链表 Runtime: 4 ms, faster than 100.00% of C++ online submissions for Merge Two Sorted Lists. class ...

  4. leetcode 21.Merge Two Sorted Lists ,java

    题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...

  5. LeetCode 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 t ...

  6. LeetCode 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 t ...

  7. Java [leetcode 21]Merge Two Sorted Lists

    题目描述: Merge two sorted linked lists and return it as a new list. The new list should be made by spli ...

  8. [LeetCode] 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 t ...

  9. Leetcode 21. Merge Two Sorted Lists(easy)

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  10. (链表) leetcode 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 t ...

随机推荐

  1. Spring声明式事务总结

    Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource.TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分. DataSo ...

  2. iOS开发基础:最新的APP打包上架流程

    之前有人留言让我更新部分文章,下面就为大家分享一下iOS的APP打包上架流程: 上传至apple developer 1.1 上传准备工作 更新上架和发布上架不同,在原始版本首次上架的时候就将描述文件 ...

  3. 如何处理IO

    Network I/O operations in user code should only be done through the Nginx Lua API calls as the Nginx ...

  4. Python 性能剖分工具

    Python 性能剖分工具 眼看着项目即将完成,却被测试人员告知没有通过性能测试,这种情况在开发中屡见不鲜.接下来的工作就是加班加点地找出性能瓶颈,然后进行优化,再进行性能测试,如此这般周而复始直到通 ...

  5. VBA find方法

    Sub Sample() Dim sfzs As New Collection Dim ws, wbs, dbs As Worksheet Dim r As Long Set ws = ThisWor ...

  6. 重写方法的利器-super

    重写方法的利器-super class ilist(list): def __init__(self,dft=None,r=list()): super(ilist, self).__init__(r ...

  7. Spark运行架构

    http://blog.csdn.net/pipisorry/article/details/52366288 1. Spark运行架构 1.1 术语定义 lApplication:Spark App ...

  8. FORM中读取图片

    1.创建ITEM 重要属性如下 item属性:图像 大小样式:调整 数据库项:否 2.读取触发器 在block级别,创建trigger READ_IMAGE_FILE('D:\'||:XX_EMOLY ...

  9. 带你深入理解STL之空间配置器(思维导图+源码)

    前不久把STL细看了一遍,由于看得太"认真",忘了做笔记,归纳和总结这步漏掉了.于是为了加深印象,打算重看一遍,并记录下来里面的一些实现细节.方便以后能较好的复习它. 以前在项目中 ...

  10. 15 ActionProvider代码例子

    Menu文件夹下代码: <menu xmlns:android="http://schemas.android.com/apk/res/android" > <! ...