-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoNumberSum.cpp
More file actions
31 lines (30 loc) · 1000 Bytes
/
TwoNumberSum.cpp
File metadata and controls
31 lines (30 loc) · 1000 Bytes
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
//两链表,如果和超过10,则考虑进位
//一个链表长,一个链表短,则短的补0;
//如果链表结束还有进位,那么还得增加一个节点来存储进位值
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
if(l1== NULL) return l2;
if(l2 == NULL) return l1;
int farward = 0;
ListNode *head = new ListNode();
ListNode* tail = head;
while(l1 || l2) {
int n1 = l1? l1-> val:0;
int n2 = l2? l2-> val:0;
int i = n1 + n2 + farward;
farward = i >= 10? 1:0;
tail -> next =new ListNode(i%10);
if(l1 != NULL)l1 = l1 -> next;
else l1 = NULL;
if(l2 != NULL)l2 = l2 -> next;
else l2 = NULL;
tail = tail -> next;
}
if(farward == 1) {
tail-> next = new ListNode(1);
tail -> next -> next = NULL;
}
return head-> next;
}
};