• [LeetCode] 21


    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

    /**
    * 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) {
        /* if one is empty, then return another */
        if(l1 == NULL) {
          return l2;
        }
        if(l2 == NULL) {
          return l1;
        }

        ListNode *t1 = l1;
        ListNode *t2 = l2;
        ListNode *tr, *result;
        tr = NULL;
        result = NULL;

        while (t1 || t2) {
          if (t1 == NULL) {
            tr->next = t2;
            break;
          }
          if (t2 == NULL) {
            tr->next = t1;
            break;
          }
          ListNode *tt;
          if (t1->val < t2->val) {
            tt = t1;
            t1 = t1->next;
          } else {
            tt = t2;
            t2 = t2->next;
          }
          if (!result) { // the fisrst time to add a nodel
            tr = tt;
            result = tt;
          } else {
            tr->next = tt;
            tr = tt;
          }
        }
        return result;
      }
    };

  • 相关阅读:
    python入门之json与pickle数据序列化
    python入门之迭代器
    python入门之生成器
    阿里云-域名免费申请ssl证书过程
    mongodb的基本命令操作
    kibana通过nginx配置访问用户验证
    Java中常用的加密算法MD5,SHA,RSA
    Weka关联规则分析
    Swing实现系统托盘
    Swing实现右下角消息框
  • 原文地址:https://www.cnblogs.com/shoemaker/p/4765850.html
Copyright © 2020-2023  润新知