zoukankan      html  css  js  c++  java
  • 合并两个有序链表

     1 /*
     2 struct ListNode {
     3     int val;
     4     struct ListNode *next;
     5     ListNode(int x) :
     6             val(x), next(NULL) {
     7     }
     8 };*/
     9 class Solution {
    10 public:
    11     ListNode* Merge(ListNode* pHead1, ListNode* pHead2)
    12     {
    13         ListNode* newhead = new ListNode(-1);
    14         ListNode* temp = newhead;
    15         if(!pHead1) return pHead2;
    16         if(!pHead2) return pHead1;
    17         while(pHead1 && pHead2){
    18             if(pHead1->val <=pHead2->val){
    19                 temp->next = pHead1;
    20                 pHead1 = pHead1->next;
    21                 temp = temp->next;
    22             }
    23             else{
    24                 temp->next = pHead2;
    25                 pHead2 = pHead2->next;
    26                 temp = temp->next;
    27             }
    28         }
    29         if(!pHead1){
    30             temp->next = pHead2;
    31         }
    32         if(!pHead2){
    33             temp->next = pHead1;
    34         }
    35         return newhead->next;
    36     }
    37 };
     1 /*
     2 struct ListNode {
     3     int val;
     4     struct ListNode *next;
     5     ListNode(int x) :
     6             val(x), next(NULL) {
     7     }
     8 };*/
     9 class Solution {
    10 public:
    11     ListNode* Merge(ListNode* pHead1, ListNode* pHead2)
    12     {
    13         ListNode* head;
    14         if(!pHead1) return pHead2;
    15         if(!pHead2) return pHead1;
    16         if(pHead1->val <= pHead2->val){
    17             head=pHead1;
    18             head->next = Merge(pHead1->next,pHead2);
    19         }
    20         else{
    21             head = pHead2;
    22             head->next = Merge(pHead1,pHead2->next);
    23         }
    24         return head;
    25     }
    26 };
  • 相关阅读:
    jython 访问数据库的方法
    Server 2008安装FTP的简单教程
    如何实现Android重启应用程序代码 ?
    android 应用程序自适应屏幕大小
    Android Dialog用法
    2008Server错误
    7种形式的Android Dialog使用举例
    ADB使用方法
    调用手机震动
    android小记之FTP文件上传
  • 原文地址:https://www.cnblogs.com/pacino12134/p/11139732.html
Copyright © 2011-2022 走看看