Skip to content

Commit a299b26

Browse files
✨ Day 7; Week 1 ✔️
1 parent 09307a3 commit a299b26

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
55
[Challenge Link](https://leetcode.com/explore/challenge/card/november-leetcoding-challenge/)
66

7-
## Week 1 🚧
7+
## Week 1
88
1. [Convert Binary Number in a Linked List to Integer](https://leetcode.com/explore/challenge/card/november-leetcoding-challenge/564/week-1-november-1st-november-7th/3516/) ➡️ [CPP Solution](Week1/getDecimalValue.cpp)
99
2. [Insertion Sort List](https://leetcode.com/explore/challenge/card/november-leetcoding-challenge/564/week-1-november-1st-november-7th/3517/) ➡️ [CPP Solution](Week1/insertionSortList.cpp)
1010
3. [Consecutive Characters](https://leetcode.com/explore/challenge/card/november-leetcoding-challenge/564/week-1-november-1st-november-7th/3518/) ➡️ [CPP Solution](Week1/maxPower.cpp)
1111
4. [Minimum Height Trees](https://leetcode.com/explore/challenge/card/november-leetcoding-challenge/564/week-1-november-1st-november-7th/3519/) ➡️ [CPP Solution](Week1/findMinHeightTrees.cpp)
1212
5. [Minimum Cost to Move Chips to The Same Position](https://leetcode.com/explore/challenge/card/november-leetcoding-challenge/564/week-1-november-1st-november-7th/3519/) ➡️ [CPP Solution](Week1/minCostToMoveChips.cpp)
1313
6. [Find the Smallest Divisor Given a Threshold](https://leetcode.com/explore/challenge/card/november-leetcoding-challenge/564/week-1-november-1st-november-7th/3520/) ➡️ [CPP Solution](Week1/smallestDivisor.cpp)
14+
7. [Add Two Numbers II](https://leetcode.com/explore/challenge/card/november-leetcoding-challenge/564/week-1-november-1st-november-7th/3521/) ➡️ [CPP Solution](Week1/addTwoNumbers.cpp)
1415

1516
## Week 2 🚧
1617
Coming soon...

Week1/addTwoNumbers.cpp

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
private:
13+
ListNode* reverseList(ListNode* head) {
14+
ListNode *curr = head, *prev = NULL, *next = NULL;
15+
16+
while(curr != NULL) {
17+
next = curr->next;
18+
curr->next = prev;
19+
20+
prev = curr;
21+
curr = next;
22+
}
23+
24+
return prev;
25+
}
26+
27+
ListNode* addList(ListNode* l1, ListNode* l2) {
28+
int sum = 0, carry = 0;
29+
ListNode *dummy = new ListNode(-1);
30+
ListNode *curr = dummy;
31+
32+
while(l1 != NULL || l2 != NULL || carry != 0) {
33+
sum = carry;
34+
35+
if(l1 != NULL) {
36+
sum += l1->val;
37+
l1 = l1->next;
38+
}
39+
40+
if(l2 != NULL) {
41+
sum += l2->val;
42+
l2 = l2->next;
43+
}
44+
45+
ListNode* newNode = new ListNode(sum % 10);
46+
curr->next = newNode;
47+
curr = curr->next;
48+
49+
carry = sum / 10;
50+
}
51+
52+
return dummy->next;
53+
}
54+
public:
55+
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
56+
l1 = reverseList(l1);
57+
l2 = reverseList(l2);
58+
59+
return reverseList(addList(l1, l2));
60+
}
61+
};

0 commit comments

Comments
 (0)