zoukankan      html  css  js  c++  java
  • 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;
    }
    /*

    */
  • 相关阅读:
    利用无线网络数据包分析无线网络安全
    C++ basic
    几道题目
    Pythonunittestddt(应用到类,实际参数化)
    Pythonunittestddt(基本应用)
    【第二章】处理数据源信息(config、excel数据源处理
    Python操作excel003(封装读取excel类
    Python+selenium 【第一章】什么叫ui自动化以及环境搭建
    【第五章】接口关联 正则表达式jsonpath模块
    【第四章】封装request类
  • 原文地址:https://www.cnblogs.com/crazyacking/p/5195998.html
Copyright © 2011-2022 走看看