You are given two linked lists representing two non-negative numbers. 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.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
给你两个链表,表示两个非负整数。数字在链表中按反序存储,例如342在链表中为2->4->3。链表每一个节点包含一个数字(0-9)。
计算这两个数字和并以链表形式返回。
无
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
/********************************* * 日期:2014-01-27 * 作者:SJF0115 * 题号: Add Two Numbers * 结果:AC * 来源:LeetCode * 总结: **********************************/ #include <iostream> #include <stdio.h> #include using namespace std; struct ListNode { int val; ListNode *next; ListNode( int x) : val(x), next(NULL) {} }; class Solution { public : ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) { ListNode *head = (ListNode *)malloc(sizeof(ListNode)); ListNode *pre = head; ListNode *node = NULL; //进位 int c = 0 ,sum; //加法 while (l1 != NULL && l2 != NULL){ sum = l1->val + l2->val + c; c = sum / 10 ; node = (ListNode *)malloc(sizeof(ListNode)); node->val = sum % 10 ; node->next = NULL; //尾插法 pre->next = node; pre = node; l1 = l1->next; l2 = l2->next; } //例如:2->4->3->1 5->6->4 while (l1 != NULL){ sum = l1->val + c; c = sum / 10 ; node = (ListNode *)malloc(sizeof(ListNode)); node->val = sum % 10 ; node->next = NULL; //尾插法 pre->next = node; pre = node; l1 = l1->next; } //例如:2->4->3 5->6->4->1 while (l2 != NULL){ sum = l2->val + c; c = sum / 10 ; node = (ListNode *)malloc(sizeof(ListNode)); node->val = sum % 10 ; node->next = NULL; //尾插法 pre->next = node; pre = node; l2 = l2->next; } //最后一位还有进位 if (c > 0 ){ node = (ListNode *)malloc(sizeof(ListNode)); node->val = c; node->next = NULL; //尾插法 pre->next = node; pre = node; } return head->next; } }; int main() { Solution solution; int A[] = { 2 , 4 , 7 , 9 }; int B[] = { 5 , 6 , 4 }; ListNode *head = NULL; ListNode *head1 = (ListNode*)malloc(sizeof(ListNode)); ListNode *head2 = (ListNode*)malloc(sizeof(ListNode)); head1->next = NULL; head2->next = NULL; ListNode *node; ListNode *pre = head1; for ( int i = 0 ;i < 4 ;i++){ node = (ListNode*)malloc(sizeof(ListNode)); node->val = A[i]; node->next = NULL; pre->next = node; pre = node; } pre = head2; for ( int i = 0 ;i < 3 ;i++){ node = (ListNode*)malloc(sizeof(ListNode)); node->val = B[i]; node->next = NULL; pre->next = node; pre = node; } head = solution.addTwoNumbers(head1->next,head2->next); while (head != NULL){ printf( "%d " ,head->val); head = head->next; } return 0 ; } </algorithm></stdio.h></iostream> |
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
|
class Solution { public : ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) { ListNode *head = (ListNode *)malloc(sizeof(ListNode)); ListNode *pre = head; ListNode *node = NULL; //进位 int c = 0 ,sum,val1,val2; //加法 while (l1 != NULL || l2 != NULL || c != 0 ){ val1 = (l1 == NULL ? 0 : l1->val); val2 = (l2 == NULL ? 0 : l2->val); sum = val1 + val2 + c; c = sum / 10 ; node = (ListNode *)malloc(sizeof(ListNode)); node->val = sum % 10 ; node->next = NULL; //尾插法 pre->next = node; pre = node; l1 = (l1 == NULL ? NULL : l1->next); l2 = (l2 == NULL ? NULL : l2->next); } return head->next; } };
|
原文:http://www.cnblogs.com/rat-bin/p/4361943.html