zoukankan      html  css  js  c++  java
  • 递归合并链表~

    #include<iostream>
    #include<map>
    using namespace std;
    
    class node{
    public:
        node():value(0),next(NULL){}
        ~node(){}
        int value;
        node* next;
    };///be careful this ;
    
    
    node* createlist(int a[],int n)
    {
     node* startnode = new node[n];
     node* ret = startnode;
     for(int i = 0;i<n;i++)
     {
         startnode[i].value = a[i];
         if(i<n-1) 
             startnode[i].next = startnode + i + 1;
     }
     return ret;
    }
    
    node* helper(node * head1,node* head2) ///这种问题都可以试着用递归来计算,好像方便的多~
    {
      if(head1 == NULL) return head2;
      if(head2 == NULL) return head1;     ///多想想递归~~~
      node* head;
      if(head1->value<head2->value)
      {
       head = head1;
       head->next = helper(head1->next,head2);
      }else
      {
       head = head2;
       head->next = helper(head1,head2->next);
      }
      return head;
    }
    
    int main()
    {
        int a[] = {1,3,5,7,9};
        int b[] = {2,4,6,8,10};
        node * t1 = createlist(a,sizeof(a)/sizeof(a[0]));
        node * t2 = createlist(b,sizeof(b)/sizeof(b[0]));
        node * head = helper(t1,t2);
        while(head)
        {cout<<" "<<head->value; head = head->next;}
    }
    berkeleysong
  • 相关阅读:
    053-509
    053-298
    053-255
    css实现省略号
    github上写blog
    解决内容被挤压缩小问题
    request.getRequestDispather().forward()与response.sendRedirect()
    资料,来自HTML5前端开发学习⑤群
    checkbox与jq<转>2
    checkbox与jq<转>
  • 原文地址:https://www.cnblogs.com/berkeleysong/p/3740992.html
Copyright © 2011-2022 走看看