-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathaddTwoNumbers.h
More file actions
59 lines (51 loc) · 1.61 KB
/
addTwoNumbers.h
File metadata and controls
59 lines (51 loc) · 1.61 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
//
// addTwoNumbers.h
// linkedList
//
// Created by junlongj on 2019/8/15.
// Copyright © 2019 junl. All rights reserved.
//
#ifndef addTwoNumbers_hpp
#define addTwoNumbers_hpp
#include <stdio.h>
/*
2.两数相加
给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。
如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。
您可以假设除了数字 0 之外,这两个数都不会以 0 开头。
示例:
输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 0 -> 8
原因:342 + 465 = 807
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/add-two-numbers
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
*/
namespace leetcode {
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
if (!l1 || !l2) return nullptr;
int flag = 0;
ListNode *head = new ListNode(0);
ListNode *ct = head;
while(l1 || l2){
int a1=0,a2=0;
if (l1){
a1=l1->val;
l1=l1->next;
}
if (l2){
a2=l2->val;
l2=l2->next;
}
int v = a1 + a2 + flag;
flag = v/10;
ListNode *node = new ListNode(v%10);
ct->next=node;
ct=ct->next;
}
if (flag)
ct->next = new ListNode(flag);
return head->next;
}
}
#endif /* addTwoNumbers_hpp */