Skip to content

Commit

Permalink
Merge pull request #206 from Rohit-Kavitake/LeetcodeSolutions
Browse files Browse the repository at this point in the history
added 2-add-two-numbers
  • Loading branch information
iamdestinychild authored Oct 7, 2023
2 parents 4c7341f + 471cd2a commit bd46351
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 0 deletions.
Binary file added LeetCode/2-add-two-numbers/C++/ScreenShot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
99 changes: 99 additions & 0 deletions LeetCode/2-add-two-numbers/C++/add-two-numbers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/**
* 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:
void reverse(ListNode* &head){
ListNode* current = head;
ListNode* prev = NULL, *next = NULL;

while (current != NULL) {
// Store next
next = current->next;
// Reverse current node's pointer
current->next = prev;
// Move pointers one position ahead.
prev = current;
current = next;
}
head = prev;

};
void print(ListNode* &head)
{
struct ListNode* temp = head;
while (temp != NULL) {
cout << temp->val << " ";
temp = temp->next;
}
};
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
// reverse(l1);
// reverse(l2);
ListNode* res = NULL;
ListNode* head1 = l1;
ListNode* head2 = l2;
int carry = 0;
while(head1 != NULL && head2 != NULL){
int data = (head1 -> val) + (head2 -> val) + carry;
if(data >= 10){
data = data - 10;
carry = 1;
}
else{
carry = 0;
}
// cout << data << "data" << endl;
ListNode* newNode = new ListNode(data);
newNode -> next = res;
res = newNode;
head1 = head1 -> next;
head2 = head2 -> next;
}
while(head1 != NULL){
int data = (head1 -> val) + carry;
if(data >= 10){
data = data - 10;
carry = 1;
}
else{
carry = 0;
}
ListNode* newNode = new ListNode(data);
newNode -> next = res;
res = newNode;
head1 = head1 -> next;

}
while(head2 != NULL){
int data = (head2 -> val) + carry;
if(data >= 10){
data = data - 10;
carry = 1;
}
else{
carry = 0;
}
ListNode* newNode = new ListNode(data);
newNode -> next = res;
res = newNode;
head2 = head2 -> next;

}
if(carry == 1){
ListNode* newNode = new ListNode(1);
newNode -> next = res;
res = newNode;
}
reverse(res);
return res;
}
};
35 changes: 35 additions & 0 deletions LeetCode/2-add-two-numbers/Problem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<h2><a href="https://leetcode.com/problems/add-two-numbers">Add Two Numbers</a></h2> <img src='https://img.shields.io/badge/Difficulty-Medium-orange' alt='Difficulty: Medium' /><hr><p>You are given two <strong>non-empty</strong> linked lists representing two non-negative integers. The digits are stored in <strong>reverse order</strong>, and each of their nodes contains a single digit. Add the two numbers and return the sum&nbsp;as a linked list.</p>

<p>You may assume the two numbers do not contain any leading zero, except the number 0 itself.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/02/addtwonumber1.jpg" style="width: 483px; height: 342px;" />
<pre>
<strong>Input:</strong> l1 = [2,4,3], l2 = [5,6,4]
<strong>Output:</strong> [7,0,8]
<strong>Explanation:</strong> 342 + 465 = 807.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> l1 = [0], l2 = [0]
<strong>Output:</strong> [0]
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
<strong>Output:</strong> [8,9,9,9,0,0,0,1]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li>The number of nodes in each linked list is in the range <code>[1, 100]</code>.</li>
<li><code>0 &lt;= Node.val &lt;= 9</code></li>
<li>It is guaranteed that the list represents a number that does not have leading zeros.</li>
</ul>

0 comments on commit bd46351

Please sign in to comment.