Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f74012c

Browse filesBrowse files
authored
Create 12 addTwoNumbers.cpp
1 parent 1394616 commit f74012c
Copy full SHA for f74012c

File tree

Expand file treeCollapse file tree

1 file changed

+36
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+36
-0
lines changed

‎January 2021/12 addTwoNumbers.cpp

Copy file name to clipboard
+36Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode() : val(0), next(nullptr) {}
7+
* ListNode(int x) : val(x), next(nullptr) {}
8+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
9+
* };
10+
*/
11+
class Solution {
12+
public:
13+
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
14+
15+
int sum=0;
16+
ListNode *l3=NULL;
17+
ListNode **node=&l3;
18+
while(l1!=NULL||l2!=NULL||sum>0)
19+
{
20+
if(l1!=NULL)
21+
{
22+
sum+=l1->val;
23+
l1=l1->next;
24+
}
25+
if(l2!=NULL)
26+
{
27+
sum+=l2->val;
28+
l2=l2->next;
29+
}
30+
(*node)=new ListNode(sum%10);
31+
sum/=10;
32+
node=&((*node)->next);
33+
}
34+
return l3;
35+
}
36+
};

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.