leetcode 445. Add Two Numbers II
来源:互联网 发布:销售管理论文数据 编辑:程序博客网 时间:2024/05/16 18:44
用栈先存着两个node的东西,再pop出来相加。
转载请注明: http://blog.csdn.net/c602273091/article/details/54317115
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { int cf = 0; stack<int> q_1; stack<int> q_2; while(l1 != NULL || l2 != NULL) { if(l1 != NULL) { q_1.push(l1->val); l1 = l1->next; } if(l2 != NULL) { q_2.push(l2->val); l2 = l2->next; } } ListNode *rel = NULL; while(!q_1.empty() && !q_2.empty()) { int a1 = q_1.top(), a2 = q_2.top(); cout << a1 << " " << a2 << endl; int sum_tmp = q_1.top() + q_2.top() + cf; q_1.pop(); q_2.pop(); cf = sum_tmp/10; sum_tmp = sum_tmp%10; ListNode *tmp = new ListNode(sum_tmp); tmp->next = rel; rel = tmp; } while(!q_1.empty()) { int tmp_q_1 = q_1.top() + cf; q_1.pop(); cf = tmp_q_1/10; tmp_q_1 = tmp_q_1 % 10; ListNode *tmp2 = new ListNode(tmp_q_1); tmp2->next = rel; rel = tmp2; } while(!q_2.empty()) { int tmp3 = q_2.top() + cf; q_2.pop(); cf = tmp3/10; tmp3 = tmp3 % 10; ListNode *tmp2 = new ListNode(tmp3); tmp2->next = rel; rel = tmp2; } if(cf) { ListNode *tmp = new ListNode(cf); tmp->next = rel; rel = tmp; } /* while(rel != NULL) { cout << rel->val << endl; rel = rel->next; }*/ return rel; }};
0 0
- LeetCode 445. Add Two Numbers II
- leetcode-445. Add Two Numbers II
- LeetCode 445. Add Two Numbers II
- [LeetCode]445. Add Two Numbers II
- LeetCode 445. Add Two Numbers II
- LeetCode 445. Add Two Numbers II
- 【LeetCode】 445. Add Two Numbers II
- LeetCode笔记:445. Add Two Numbers II
- leetcode 445. Add Two Numbers II
- 【Leetcode】445. Add Two Numbers II
- LeetCode 445. Add Two Numbers II
- LeetCode 445. Add Two Numbers II
- Leetcode-445. Add Two Numbers II
- LeetCode 445. Add Two Numbers II
- leetcode 445. Add Two Numbers II
- leetcode 445. Add Two Numbers II
- [LeetCode] 445. Add Two Numbers II
- [LeetCode]445. Add Two Numbers II
- MySQL 的索引
- github基本使用
- Zbrush4r7|3D建模雕刻软件
- nginx和apache下的url rewrite
- 顺序表存储结构
- leetcode 445. Add Two Numbers II
- MySQL主从复制技术(纯干货)
- 轮回 第十章 醒来,吃馒头?
- Tensorflow的Bazel编程(四)
- platform之按键驱动多种写法(笔记)
- Effective c++ 读书笔记 4
- 命令模式
- 2318 Mod(哈尔滨理工大学)
- Android okHttp网络请求之Get/Post请求