Add Two Numbers II问题及解法
2017-09-03 10:36
363 查看
问题描述:
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.
Follow up:
What if you cannot modify the input lists? In other words, reversing the lists is not allowed.
示例:
问题分析:
两数相加问题,注意考虑溢出问题,这里采用字符串的加减法。
过程详见代码:
/**
* 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) {
string res1, res2;
while (l1)
{
res1.push_back(l1->val + '0');
l1 = l1->next;
}
while (l2)
{
res2.push_back(l2->val + '0');
l2 = l2->next;
}
int flag = 0,t;
int i = res1.size() - 1,j = res2.size()- 1;
ListNode * curNode, *head = nullptr;
while (i >= 0 || j >= 0)
{
if (i >= 0 && j >= 0)
{
t = res1[i] - '0' + res2[j] - '0' + flag;
i--;
j--;
}
else if (i >= 0)
{
t = res1[i] - '0' + flag;
i--;
}
else
{
t = res2[j] - '0' + flag;
j--;
}
flag = t / 10;
curNode = new ListNode(t % 10);
curNode->next = head;
head = curNode;
}
if (flag)
{
curNode = new ListNode(flag);
curNode->next = head;
head = curNode;
}
return head;
}
};
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.
Follow up:
What if you cannot modify the input lists? In other words, reversing the lists is not allowed.
示例:
Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 8 -> 0 -> 7
问题分析:
两数相加问题,注意考虑溢出问题,这里采用字符串的加减法。
过程详见代码:
/**
* 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) {
string res1, res2;
while (l1)
{
res1.push_back(l1->val + '0');
l1 = l1->next;
}
while (l2)
{
res2.push_back(l2->val + '0');
l2 = l2->next;
}
int flag = 0,t;
int i = res1.size() - 1,j = res2.size()- 1;
ListNode * curNode, *head = nullptr;
while (i >= 0 || j >= 0)
{
if (i >= 0 && j >= 0)
{
t = res1[i] - '0' + res2[j] - '0' + flag;
i--;
j--;
}
else if (i >= 0)
{
t = res1[i] - '0' + flag;
i--;
}
else
{
t = res2[j] - '0' + flag;
j--;
}
flag = t / 10;
curNode = new ListNode(t % 10);
curNode->next = head;
head = curNode;
}
if (flag)
{
curNode = new ListNode(flag);
curNode->next = head;
head = curNode;
}
return head;
}
};
相关文章推荐
- Add Two Numbers问题及解法
- Maximum XOR of Two Numbers in an Array问题及解法
- [LeetCode] Add Two Numbers II 两个数字相加之二
- [c++] LeetCode Add Two Numbers问题
- 445-Add Two Numbers II
- LeetCode #445: Add Two Numbers II
- LeetCode算法问题1 —— Add Two Numbers
- leetcode445 Add Two Numbers II java
- LeetCode Add Two Numbers II
- 大整数相加--链表结构正向存储--Add Two Numbers II
- 数字加和问题: plus one , Add Binary, Add Two Numbers
- Sum—LeetCode-445 Add Two Numbers II
- Leetcode Day6 2. Add Two Numbers I & II
- 链表求和问题(add two numbers)
- leetcode 445 Add Two Numbers II C++
- Populating Next Right Pointers in Each Node I,II,Add Two Numbers,Integer to Roman
- 【Leetcode问题搬运翻译】Add two numbers
- LinkedList-445-Add Two Numbers II
- Add Two Numbers II
- Add Two Numbers II