Skip to content

Commit b3c41cb

Browse files
Added solution for adding number II
1 parent 4c02ca4 commit b3c41cb

File tree

3 files changed

+162
-0
lines changed

3 files changed

+162
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ Happy to accept any contributions to this in any langugage.
6262
3. [Leet Code 297 - Serialize Deserialize Binary Tree](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/) | [Solution](./level_hard/SerializeDeserializeBinaryTree.ts)
6363
4. [Leet Code 428 - Serialize Deserialize N-Ary Tree](https://leetcode.com/problems/serialize-and-deserialize-n-ary-tree/) | [Solution](./level_hard/SerializeDeserializeNAryTree.ts)
6464
5. [Leet Code 25 - Reverse Linked List k Nodes at a time](https://leetcode.com/problems/reverse-nodes-in-k-group/) | [Solution](./level_hard/LeetCode%2025_Reverse%20Linked%20List%20K%20Nodes.ts)
65+
6. [Leet Code 445 - Add Numbers II](https://leetcode.com/problems/add-two-numbers-ii/) | [Solution 1](./level_medium/LeetCode%20445_Add%202%20Numbers_1.ts) | [Solution 2](./level_medium/LeetCode%20445_Add%202%20Numbers_2.ts)
6566

6667
## Sorting
6768
1. Bubble Sort | [Solution](./sorting/Sort_Bubble.js)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* class ListNode {
4+
* val: number
5+
* next: ListNode | null
6+
* constructor(val?: number, next?: ListNode | null) {
7+
* this.val = (val===undefined ? 0 : val)
8+
* this.next = (next===undefined ? null : next)
9+
* }
10+
* }
11+
*/
12+
/**
13+
* Leet Code 445.
14+
* Solution uses O(n) space for each linked list to store the reverse items into a stack, so that the least
15+
* significant digit can be read first and so on. Alternatively, you can create another linked list in reverse
16+
* order to reverse it in place.
17+
*
18+
* Time: O(n) for navigation (n max of all items in the linked lists)
19+
* Space: O(n) for storing all items in reverse apart from the final numbers.
20+
*
21+
* @param l1
22+
* @param l2
23+
*/
24+
function addTwoNumbers(l1: ListNode | null, l2: ListNode | null): ListNode | null {
25+
const l1Stack: number[] = createStack(l1);
26+
const l2Stack: number[] = createStack(l2);
27+
const result: number[] = [];
28+
let carry = 0;
29+
while(l1Stack.length > 0 || l2Stack.length > 0) {
30+
const sum = (l1Stack.pop() || 0) + (l2Stack.pop() || 0) + carry;
31+
carry = Math.floor(sum / 10);
32+
result.push(sum % 10);
33+
}
34+
35+
if (carry > 0) result.push(carry);
36+
return stackToList(result);
37+
};
38+
39+
function stackToList(result: number[]) {
40+
const head: ListNode = new ListNode(result.pop(), null);
41+
let curr = head;
42+
while(result.length > 0) {
43+
const item = result.pop();
44+
curr.next = new ListNode(item, null);
45+
curr = curr.next;
46+
}
47+
return head;
48+
}
49+
50+
function createStack(l1: ListNode | null, stack: number[] = []): number[] {
51+
if (l1 === null) return stack
52+
stack.push(l1.val)
53+
return createStack(l1.next, stack)
54+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* class ListNode {
4+
* val: number
5+
* next: ListNode | null
6+
* constructor(val?: number, next?: ListNode | null) {
7+
* this.val = (val===undefined ? 0 : val)
8+
* this.next = (next===undefined ? null : next)
9+
* }
10+
* }
11+
*/
12+
13+
/**
14+
* Leet Code 445.
15+
*
16+
* Solution uses an O(1) space aimed to reduce the required space for reversal or virtual reversal. It rather uses
17+
* the length of the linked list to identify when a padding can be used and when can it be skipped. This step is done
18+
* without the help of additional carry, since carry needs to be added in the reverse order.
19+
*
20+
* To adjust the carry pointer, we need to reverse the list, adjust the carry and reverse again.
21+
*
22+
* O(n) time for run-time, O(1) for space.
23+
*
24+
* @param l1
25+
* @param l2
26+
*/
27+
function addTwoNumbers(l1: ListNode | null, l2: ListNode | null): ListNode | null {
28+
29+
// Finding the length of each linked list.
30+
let l1Length = length(l1);
31+
let l2Length = length(l2);
32+
33+
let resultCurr: ListNode | null = new ListNode(0, null), resultHead = resultCurr, lastCurr: ListNode | null = resultCurr;
34+
35+
// Adding items with prefixes attached, not using the carry.
36+
while(l1Length >= 0 && l2Length >= 0 && l1 !== null && l2 !== null) {
37+
if (l1Length > l2Length) {
38+
// L1 has more items than l2, lets pull from l1 and add 0 for l2.
39+
resultCurr.val = l1.val;
40+
l1 = l1.next;
41+
resultCurr.next = new ListNode(0, null)
42+
l1Length--;
43+
} else if (l1Length < l2Length) {
44+
// L2 has more items than l1, lets pull from l2 and add 0 for l1.
45+
resultCurr.val = l2.val;
46+
l2 = l2.next;
47+
resultCurr.next = new ListNode(0, null)
48+
l2Length--
49+
} else {
50+
// Both have equal items, lets add.
51+
resultCurr.val = l2.val + l1.val;
52+
l2 = l2.next; l1 = l1.next;
53+
resultCurr.next = new ListNode(0, null)
54+
l2Length--; l1Length--;
55+
}
56+
57+
lastCurr = resultCurr;
58+
resultCurr = resultCurr.next;
59+
}
60+
lastCurr.next = null;
61+
62+
let newHead = reverseLinkedList(resultHead);
63+
64+
// Adjust the carry now.
65+
adjustCarry(newHead)
66+
67+
// Reverse the list and return.
68+
newHead = reverseLinkedList(newHead);
69+
70+
return newHead;
71+
};
72+
73+
function adjustCarry(list: ListNode | null) {
74+
let curr = list, last = null, carry = 0;
75+
while(curr !== null) {
76+
const val = curr.val + carry;
77+
curr.val = val % 10;
78+
carry = Math.floor(val / 10)
79+
last = curr;
80+
curr = curr.next;
81+
}
82+
83+
// Add the pending carry to a new node.
84+
if (carry > 0 && last !== null) {
85+
last.next = new ListNode(carry, null);
86+
}
87+
}
88+
89+
function length(l1: ListNode | null) {
90+
let length = 0, curr = l1;
91+
while(curr !== null) {
92+
length++
93+
curr = curr.next;
94+
}
95+
return length;
96+
}
97+
98+
function reverseLinkedList(l1: ListNode | null) {
99+
let curr = l1, last: ListNode | null = null;
100+
while(curr !== null) {
101+
const next = curr.next;
102+
curr.next = last;
103+
last = curr;
104+
curr = next;
105+
}
106+
return last;
107+
}

0 commit comments

Comments
 (0)