-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.addTwoNumbers.cpp
More file actions
36 lines (34 loc) · 902 Bytes
/
2.addTwoNumbers.cpp
File metadata and controls
36 lines (34 loc) · 902 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
32
33
34
35
36
/*
* @lc app=leetcode id=2 lang=cpp
*
* [2] Add Two Numbers
*/
// @lc code=start
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution
{
public:
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2)
{
// 先判断各种特殊情况
if (l1 == nullptr) return l2;
if (l2 == nullptr) return l1;
// 链表用循环写需要处理头结点,用递归会方便很多
int temp = l1->val + l2->val;
ListNode* p = new ListNode(temp%10);
p->next = addTwoNumbers(l1->next, l2->next);
if (temp/10)
p->next = addTwoNumbers(p->next, new ListNode(1));
return p;
}
};
// @lc code=end