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

    */
  • 相关阅读:
    解决RuntimeError: cuda runtime error (30) : unknown error at /pytorch/aten/src/THC/THCGeneral.cpp:70&NVIDIA-SMI has failed because it couldn't communicate with the NVIDIA driver. Make sure that the late
    查看Ubuntu/Anaconda/cuda/cudnn/Tensorflow/Pytorch版本
    创建新用户
    142. Linked List Cycle II
    网页版Instagram如何发照片
    141. Linked List Cycle
    将博客搬至CSDN
    92. Reverse Linked List II
    Dlib使用过程全记录(一)
    定制new和delete
  • 原文地址:https://www.cnblogs.com/crazyacking/p/5195998.html
Copyright © 2011-2022 走看看