• 合并两个有序链表


     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 };
  • 相关阅读:
    scrapy框架
    selenium解析
    xpath解析
    解析语法
    request-html-render
    牛逼的requests-html
    Beautifulsoup
    请求和响应
    reuqests请求
    Django文件上传下载与富文本编辑框
  • 原文地址:https://www.cnblogs.com/pacino12134/p/11139732.html
Copyright © 2020-2023  润新知