445-Add Two Numbers II
来源:互联网 发布:妇产科网络咨询成本 编辑:程序博客网 时间:2024/05/19 14:51
[难度] medium
[分类] linked list
1.题目描述
You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
2.测试样例
Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 8 -> 0 -> 7
3.算法分析
输入是两个链表,要将链表中的数字从右向左对应相加,最终得到结果链表。
(1)将输入的两个链表进行反转,采用栈实现,先将链表中的结点从左到右push到栈中,然后将栈中的节点pop即可得到相应的反转链表。
(2)将反转后的链表对应数字进行相加,注意进位的处理。
(3)得到的链表再次利用栈进行反转即可得到最终结果。
4.代码实现
class Solution {public: ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { stack<ListNode*> mystack; // get reversed list l1 ListNode* p = l1; while (p != NULL) { mystack.push(p); p = p->next; } if (!mystack.empty()) { p = mystack.top(); mystack.pop(); l1 = p; } while (!mystack.empty()) { p->next = mystack.top(); mystack.pop(); p = p->next; } if (p != NULL) p->next = NULL; // get reversed list l2 ListNode* p2 = l2; while (p2 != NULL) { mystack.push(p2); p2 = p2->next; } if (!mystack.empty()) { p2 = mystack.top(); mystack.pop(); l2 = p2; } while (!mystack.empty()) { p2->next = mystack.top(); mystack.pop(); p2 = p2->next; } if (p2 != NULL) p2->next = NULL; // sum correspond num, notice the carry int carry = 0; ListNode* head = NULL; if (l1 != NULL && l2 != NULL) { carry = (l1->val + l2->val) / 10; head = new ListNode((l1->val + l2->val)%10); l1 = l1->next; l2 = l2->next; } else if (l1 != NULL && l2 == NULL) { head = new ListNode(l1->val); l1 = l1->next; } else if (l1 == NULL && l2 != NULL) { head = new ListNode(l2->val); l2 = l2->next; } ListNode* result = head; int sum = 0; while (l1 != NULL || l2 != NULL) { if (l1 != NULL && l2 != NULL) { if (carry == 1) sum = l1->val + l2->val + 1; else sum = l1->val + l2->val; carry = sum / 10; head->next = new ListNode(sum % 10); l1 = l1->next; l2 = l2->next; } else if (l1 != NULL && l2 == NULL) { head->next = new ListNode((l1->val + carry)%10); carry = (l1->val + carry) / 10; l1 = l1->next; } else if (l1 == NULL && l2 != NULL) { head->next = new ListNode((l2->val + carry)%10); carry = (l2->val + carry) / 10; l2 = l2->next; } head = head->next; } // if the final carry is one if (carry == 1) { head->next = new ListNode(1); head = head->next; } head->next = NULL; // reverse the final list and get result while (result != NULL) { mystack.push(result); result = result->next; } if (!mystack.empty()) { result = mystack.top(); mystack.pop(); head = result; } while (!mystack.empty()) { head->next = mystack.top(); mystack.pop(); head = head->next; } head->next = NULL; return result; }};
main函数(用于测试):
void print(ListNode* l1) { cout << l1->val; l1 = l1->next; int count = 1; while (l1 != NULL) { cout << " -> " << l1->val; l1 = l1->next; count++; } cout << " length: " << count << endl;}int main() { int n1, n2; cout << "len1 and len2:"; cin >> n1 >> n2; int num; ListNode* head = NULL; ListNode* l1 = NULL; ListNode* l2 = NULL; int i = n1, j = n2; while (i--) { cin >> num; if (i == (n1 -1)) { head = new ListNode(num); l1 = head; } else { head->next = new ListNode(num); head = head->next; } } while (j--) { cin >> num; if (j == (n2-1)) { head = new ListNode(num); l2 = head; } else { head->next = new ListNode(num); head = head->next; } } print(l1); print(l2); ListNode* temp = addTwoNumbers(l1, l2); if (temp == NULL) { cout << "here" << endl; system("pause"); return 0; } print(temp); system("pause"); return 0;}
5.小结
在处理链表的时候,要特别注意对head结点单独进行处理,还有将最后结点的next设置为NULL,链表的处理比较容易出错,要特别注意链表为空的情况并对其进行相应的处理。
阅读全文
0 0
- 445-Add Two Numbers II
- Add Two Numbers II
- Add Two Numbers II
- Add Two Numbers II
- Add Two Numbers II
- Medium 445题 Add Two Numbers II
- Leetcode Add Two Numbers II 445
- leetcode 445 Add Two Numbers II C++
- LeetCode #445: Add Two Numbers II
- Leetcode#445(链表)-Add Two Numbers II
- 【leetcode 445】Add Two Numbers II
- LeetCode.2(445) Add Two numbers(II)
- LeetCode#445 Add Two Numbers II (week15)
- 445. Add Two Numbers II
- [LeetCode]Add Two Numbers II
- Add Two Numbers II ---LeetCode
- 445. Add Two Numbers II
- LeetCode445. Add Two Numbers II
- JavaScript 开发者经常忽略或误用的七个基础知识点
- OkHttp wiki官方文档翻译(二)
- jpa query 返回map(一)
- 域名如何解析到指定IP?
- sql之left join、right join、inner join的区别
- 445-Add Two Numbers II
- win8 eclipse搭建stm32的开发环境—首篇(新手必看)
- Linux 安装redis及redis扩展
- 评论项目测试经验
- android 获取本地图片路径失败,cursor.getString(column_index);返回null
- 微信小程序 —— 动态决定页面元素显示或隐藏的技巧
- 代理模式(结构型设计模式)
- Handler三秒跳转
- 函数的四种调用模式