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;
    }
    /*

    */
  • 相关阅读:
    MyBatis3.2从入门到精通第一章
    (转)浅析Java中的访问权限控制
    mysql添加索引命令
    (转)浅谈Java中的对象和对象引用
    (转)String、StringBuffer与StringBuilder之间区别
    (转)浅谈Java中的equals和==
    Java并发编程:Lock
    Java并发编程:synchronized
    安装MySQL
    Excel常用函数
  • 原文地址:https://www.cnblogs.com/crazyacking/p/5195998.html
Copyright © 2011-2022 走看看