• LeetCode


    21. Merge Two Sorted Lists 

    Problem's Link

     ----------------------------------------------------------------------------

    Mean: 

    将两个非递减排列的链表合并成一个链表,所得链表依然按照非递减顺序排列.

    analyse:

    链表的基本操作.

    Time complexity: O(N)

     

    view code

    /**
    * -----------------------------------------------------------------
    * Copyright (c) 2016 crazyacking.All rights headerved.
    * -----------------------------------------------------------------
    *       Author: crazyacking
    *       Date  : 2016-02-17-16.50
    */
    #include <queue>
    #include <cstdio>
    #include <set>
    #include <string>
    #include <stack>
    #include <cmath>
    #include <climits>
    #include <map>
    #include <cstdlib>
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <cstring>
    using namespace std;
    typedef long long(LL);
    typedef unsigned long long(ULL);
    const double eps(1e-8);

    //  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 *res=NULL,*head=NULL;
           bool isFirst=true;
           while(l1 && l2)
           {
               if(l1->val < l2->val)
               {
                   if(isFirst)
                   {
                       head=new ListNode(l1->val);
                       res=head;
                       isFirst=false;
                   }
                   else
                   {
                       head->next=new ListNode(l1->val);
                       head=head->next;
                   }
                   l1=l1->next;
               }
               else
               {
                   if(isFirst)
                   {
                       head=new ListNode(l2->val);
                       res=head;
                       isFirst=false;
                   }
                   else
                   {
                       head->next=new ListNode(l2->val);
                       head=head->next;
                   }
                   l2=l2->next;
               }
           }

           while(l1)
           {
               if(isFirst)
               {
                   head=new ListNode(l1->val);
                   res=head;
                   isFirst=false;
               }
               else
               {
                   head->next=new ListNode(l1->val);
                   head=head->next;
               }
               l1=l1->next;
           }
           while(l2)
           {
               if(isFirst)
               {
                   head=new ListNode(l2->val);
                   res=head;
                   isFirst=false;
               }
               else
               {
                   head->next=new ListNode(l2->val);
                   head=head->next;
               }
               l2=l2->next;
           }
           return res;
       }
    };

    int main()
    {

       return 0;
    }
    /*

    */
  • 相关阅读:
    让Dreamweaver支持cshtml (MVC Razor环境)
    href="#"与href="javascript:void(0)"的区别
    ASP.NET MVC 4 中Razor 视图中JS无法调试 (重要)
    03011_HttpServletRequest
    1004. 成绩排名 (20)
    1003. 我要通过!(20)
    C语言文件读写命令fprintf和fscanf
    1002. 写出这个数 (20)
    1001. 害死人不偿命的(3n+1)猜想 (15)
    汇编in和out实例解析
  • 原文地址:https://www.cnblogs.com/crazyacking/p/5195998.html
Copyright © 2020-2023  润新知