zoukankan      html  css  js  c++  java
  • LeetCode Merge Two Sorted Lists

     1 /**
     2  * Definition for singly-linked list.
     3  * struct ListNode {
     4  *     int val;
     5  *     ListNode *next;
     6  *     ListNode(int x) : val(x), next(NULL) {}
     7  * };
     8  */
     9 class Solution {
    10 public:
    11     ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
    12         // Start typing your C/C++ solution below
    13         // DO NOT write int main() function
    14         ListNode head(0);
    15         ListNode *p=&head;
    16         while(l1!=NULL&&l2!=NULL)
    17         {
    18             if(l1->val<l2->val)
    19             {
    20                 p->next=l1;
    21                 p=p->next;
    22                 l1=l1->next;
    23             }
    24             else
    25             {
    26                 p->next=l2;
    27                 p=p->next;
    28                 l2=l2->next;
    29             }
    30         }
    31         if(l1)
    32         {
    33             p->next=l1;
    34         }
    35         if(l2)
    36         {
    37             p->next=l2;
    38         }
    39         return head.next;
    40     }
    41 };
  • 相关阅读:
    struts2.0利用注解上传和下载图片
    hibernate @ManyToOne
    Cookie会话管理
    ServletContext
    Servlet 1
    ArrayList
    BigInteger类和BigDecimal类
    Math类
    System类
    基本类型包装类
  • 原文地址:https://www.cnblogs.com/mengqingzhong/p/3125095.html
Copyright © 2011-2022 走看看