-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2. Add Two Numbers.cpp
52 lines (37 loc) · 1.87 KB
/
2. Add Two Numbers.cpp
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
/*
Link: https://leetcode.com/problems/add-two-numbers/
You are given two non-empty linked lists representing two non-negative integers. 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.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Example:
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.
This file is also updated.
*/
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode* head = new ListNode(0); // new node to return the sum
ListNode* ptr = head; // pointer to traverse
int carry=0, sm, n1, n2;
while (l1 || l2) { // traverse while both of the linked lists are not null
n1 = (l1) ? l1->val : 0; // in case one l1 become null its value will be treated as 0
n2 = (l2) ? l2->val : 0;
sm = n1 + n2 + carry;
ptr->next = new ListNode(sm % 10); // creating a new node with the ones place of the sum and assigning it to next of ptr
ptr = ptr->next; // moving ptr to its next
carry = sm / 10; // carry
if (l1) {
l1 = l1->next;
}
if (l2) {
l2 = l2->next;
}
}
if(carry) { // when both the list are null but there is a carry left
ptr->next = new ListNode(carry); // create a new node and assign it the next of current ptr.
}
return head->next; // return next of head as there a node at the begining containing 0.
}
/*
Time Complexity: O(max(m,n)) m & n are the length of the lists.
Space Complexity: O(max(m,n))
*/