forked from changqing16/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.cpp
More file actions
93 lines (90 loc) · 1.78 KB
/
2.cpp
File metadata and controls
93 lines (90 loc) · 1.78 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <stdio.h>
#include <string>
//大数相加
struct ListNode
{
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2)
{
ListNode *head, *pre, *p, *p1 = l1, *p2 = l2, *p3;
int temp, f = 0;
head = new ListNode(-1);
pre = head;
while (p1 != NULL && p2 != NULL)
{
temp = p1->val + p2->val + f;
if (temp > 9)
f = 1;
else
f = 0;
p = new ListNode(temp % 10);
pre->next = p;
pre = p;
p1 = p1->next;
p2 = p2->next;
}
if (p1 != NULL)
p3 = p1;
else
p3 = p2;
while (p3 != NULL)
{
temp = p3->val + f;
if (temp > 9)
f = 1;
else
f = 0;
p = new ListNode(temp % 10);
pre->next = p;
pre = p;
p3 = p3->next;
}
if (f)
{
p = new ListNode(1);
pre->next = p;
}
head = head->next;
return head;
}
// ListNode *addTwoNumbers(ListNode *l1, ListNode *l2)
// {
// int sum = 0, tmp;
// sum += add(l1);
// sum += add(l2);
// ListNode *head, *pre;
// head = new ListNode(sum % 10);
// sum /= 10;
// pre = head;
// while (sum)
// {
// tmp = sum % 10;
// ListNode *p = new ListNode(tmp);
// pre->next = p;
// pre = p;
// sum /= 10;
// }
// return head;
// }
// int add(ListNode *head)
// {
// int sum = 0, temp, cur = 0;
// while (head != NULL)
// {
// temp = head->val;
// for (int i = 0; i < cur; i++)
// {
// temp *= 10;
// }
// sum += temp;
// head = head->next;
// cur++;
// }
// return sum;
// }
// int main()
// {
// }