Skip to content

Commit 565ec1b

Browse files
authored
Merge pull request AlgorithmCrackers#35 from AlgorithmCrackers/add-two-numbers
Add two numbers
2 parents d984ea5 + bde0f0a commit 565ec1b

File tree

4 files changed

+72
-10
lines changed

4 files changed

+72
-10
lines changed

02_LinkedLists/LoopInList/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ bool hasLoop(struct node *head)
2727
2828
> The entry node is the first node in the loop from head of list. For instance, the entry node of loop in the list of Figure 1 is the node with value 3.
2929
30-
#### [LeetCode Solution](https://leetcode.com/problems/linked-list-cycle/)
30+
#### [LeetCode link](https://leetcode.com/problems/linked-list-cycle/)
3131
3232
#### [Detailed discussion](http://k2code.blogspot.in/2010/04/how-would-you-detect-loop-in-linked.html)

02_LinkedLists/README.md

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,7 @@
77
* Input: the node `c` from the linked list `a->b->c->d->e`
88
* Result: nothing is returned, but the new linked list looks like `a->b->d->e`
99
4. Write code to partition a linked list around a value `x`, such that all nodes less than `x` come before all nodes greater than or equal to `x`.
10-
5. You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in reverse order, such that the 1's digit is at the head of the list. Write a function that adds the two numbers and returns the sum as a linked list.
11-
* EXAMPLE
12-
* Input: (7->1->6) + (5->9->2). That is, 617 + 295.
13-
* Output: 2->1->9. That is, 912.
14-
* FOLLOW UP
15-
* Suppose the digits are stored in forward order. Repeat the above problem.
16-
* Example
17-
* Input: (6->1->7) + (2->9->5). That is, 617 + 295.
18-
* Output: 9->1->2. That is, 912.
10+
5. 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.**[add-two-numbers](add-two-numbers)**
1911
6. Given a circular linked list, implement an algorithm which returns node at the beginning of the loop.**[LoopInList](LoopInList)**
2012
* DEFINITION
2113
* Circular linked list: A (corrupt) linked list in which a node’s next pointer points to an earlier node, so as to make a loop in the linked list.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Add Two Numbers
2+
3+
`Medium`
4+
5+
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.
6+
7+
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
8+
9+
## Example:
10+
11+
```
12+
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
13+
Output: 7 -> 0 -> 8
14+
Explanation: 342 + 465 = 807.
15+
```
16+
17+
## Complexity Analysis:
18+
19+
- **Time complexity** : `O(max(m, n))`. Assume that `m` and `n` are lengths of the two input linked-lists respectively. We traverse the linked-list with the maximum length once.
20+
21+
- **Space complexity** : `O(max(m, n))`. The length of the new list is the maximum length of the two lists + 1 (for the carry over).
22+
23+
#### [LeetCode link](https://leetcode.com/problems/add-two-numbers/)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val) {
4+
* this.val = val;
5+
* this.next = null;
6+
* }
7+
*/
8+
/**
9+
* @param {ListNode} l1
10+
* @param {ListNode} l2
11+
* @return {ListNode}
12+
*/
13+
const addTwoNumbers = function(l1, l2) {
14+
let current = new ListNode(-1)
15+
const ptrToFake = current
16+
// variable to store the carry over after addition (either 1 or 0)
17+
let carry = 0
18+
while(l1 || l2) {
19+
const a = l1 ? l1.val : 0
20+
const b = l2 ? l2.val : 0
21+
// actual summing
22+
sum = carry + a + b
23+
// find the carry over
24+
carry = sum > 9 ? 1 : 0
25+
// the mod to 10 is the required number to store in the node
26+
// example: 9 + 8 = 17, here 7 will be in the node, 1 goes to carry over
27+
const node = new ListNode(sum % 10)
28+
// add to the resulting linked list
29+
current.next = node
30+
// iterate to the next nodes
31+
current = node
32+
if (l1) {
33+
l1 = l1.next
34+
}
35+
if (l2) {
36+
l2 = l2.next
37+
}
38+
}
39+
40+
// finally, check if the carry was left over.. this means we need an extra 1
41+
if (carry) {
42+
const node = new ListNode(1)
43+
current.next = node
44+
}
45+
46+
return ptrToFake.next
47+
};

0 commit comments

Comments
 (0)