-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
66 lines (62 loc) · 1.93 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
//Definition for singly-linked list.
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
//链表加法
/*The pseudocode is as following:
Initialize current node to dummy head of the returning list.
Initialize carry to 0.
Initialize p and q to head of l1 and l2 respectively.
Loop through lists l1 and l2 until you reach both ends.
Set x to node p's value. If p has reached the end of l1, set to 0.
Set y to node q's value. If q has reached the end of l2, set to 0.
Set sum = x + y + carry.
Update carry = sum / 10.
Create a new node with the digit value of (sum mod 10) and set it to current node's next, then advance current node to next.
Advance both p and q.
Check if carry = 1, if so append a new node with digit 11 to the returning list.
Return dummy head's next node.
*/
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode *cur1,*cur2;
ListNode *l3,*cur3;
l3 = new ListNode(0);
cur1 = l1;
cur2 = l2;
cur3 = l3;
int carry = 0; // 考虑进位情况,将进位的值存储
while(cur1!=NULL || cur2!=NULL){
int x = (cur1 != NULL) ? cur1->val : 0;
int y = (cur2 != NULL) ? cur2->val : 0;
int sum = x + y + carry;
carry = sum / 10; //每次更新进位值 如果只判断sum>10,有些情况会出错
cur3->next = new ListNode(sum%10);
cur3 = cur3->next;
if(cur1 != NULL){
cur1 = cur1 -> next;
}
if(cur2 != NULL){
cur2 = cur2 -> next;
}
}
if(carry > 0){
cur3 -> next = new ListNode(carry);
}
return l3->next;
}
};
int main()
{
cout << "Hello world!" << endl;
return 0;
}