Leetcode #2
来源:互联网 发布:电脑微信群发软件 编辑:程序博客网 时间:2024/04/23 14:53
题目
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order 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.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
坑:太久没写链表了,竟然犯了在函数内部定义节点的错误,ListNode head = ListNode(0); ×
应该使用new!!! 临时变量无法取址,报错 ‘’error: taking address of temporary[-fpermissive]‘’
class Solution {public: ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { ListNode* head = new ListNode(0); ListNode* current = head; int carry = 0; int sum = 0; while ((l1 != NULL) || (l2 != NULL) || carry) { sum = carry; carry = 0; if (l1 != NULL) { sum += l1->val; l1 = l1->next; } if (l2 != NULL) { sum += l2->val; l2 = l2->next; } if (sum > 9) { carry = 1; sum -= 10; } current->next = new ListNode(sum); current = current->next; } return head->next; }};
阅读全文
0 0
- LeetCode(2)
- Leetcode 2
- leetcode 2
- leetcode 2
- LeetCode(2)
- Leetcode 2
- LeetCode(2)
- Leetcode 2
- leetcode-2
- LeetCode 2
- leetcode(2)
- LeetCode(2)
- leetcode 2
- leetcode(2)
- Leetcode #2
- LeetCode-2
- [Leetcode] 2 Sum
- [LeetCode] Unique Paths 2
- IDEA下maven编译打包Java项目成jar包但是resource下配置文件打包不成功
- java小程序——豆机
- 【稳定方案】PS-6M40-C LVDS转VBO 4K板卡
- 用类描述电脑CPU的速度和硬盘的容量
- [Codevs P1391]伊吹萃香
- Leetcode #2
- 学习rocketmq(1)安装启动入门,是rocketmq可以外网访问
- python lambda
- C语言——简单的相关测试题
- nginx+php上传大文件
- TCP/IP协议-为什么说TCP是可靠连接
- 计算机发展
- .html文件是白色的,而不是浏览器的图标
- 基于CentOS搭建个人网盘