-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #206 from Rohit-Kavitake/LeetcodeSolutions
added 2-add-two-numbers
- Loading branch information
Showing
3 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 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> </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> </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 <= Node.val <= 9</code></li> | ||
<li>It is guaranteed that the list represents a number that does not have leading zeros.</li> | ||
</ul> |