zoukankan      html  css  js  c++  java
  • 2. Add Two Numbers

    #include <string>
    #include <stack>
    #include <vector>
    #include <map>
    #include <algorithm>
    using namespace std;
    struct ListNode {
        int val;
        ListNode *next;
        ListNode(int x) : val(x), next(NULL) {}
       
    };
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        string s1;
        string s2;
        while (l1 != NULL) {
            s1.push_back(l1->val + '0');
            l1 = l1->next;
        }
        reverse(s1.begin(), s1.end());
        long long a = stoll(s1);
        while (l2 != NULL) {
            s2.push_back(l2->val + '0');
            l2 = l2->next;
        }
        reverse(s2.begin(), s2.end());
        long long b = stoll(s2);
        string temp = to_string(a + b);
        ListNode* res2 = new ListNode(temp[temp.size()-1] - '0');
        ListNode* res1 = res2;
        for (int i = temp.size()-2; i >=0; i--) {
            res2->next = new ListNode(temp[i] - '0');
            res2 = res2->next;
        }
        return res1;
    }
    int main() {
        ListNode* l1 = new ListNode(5);
        //l1->next= new ListNode(1);
        ListNode* l2 = new ListNode(5);
        ListNode* res = addTwoNumbers(l1, l2);
    }

  • 相关阅读:
    MySQL正则表达式 REGEXP详解
    INSERT DELAYED 句法
    mysql查询语句分析 explain用法
    mysql导出导入
    sf04_操作系统中 heap 和 stack 的区别
    2.4 Rust Ownership
    2.1 GO 变量定义
    1.3 IDAE 中使用GO开发项目
    my30_表碎片整理
    my29_PXC集群状态查看
  • 原文地址:https://www.cnblogs.com/hustlx/p/5251023.html
Copyright © 2011-2022 走看看