|
| 1 | +// Sum Lists: You have two numbers represented by a linked list, where each node contains a single digit. The digits are |
| 2 | +// stored in reverse order, such that the 1's digit is at the head of the list. Write a function that adds the two |
| 3 | +// numbers and returns the sum as a linked list. |
| 4 | +// Example |
| 5 | +// Input: (7-> 1 -> 6) + (5 -> 9 -> 2). That is, 617 + 295. |
| 6 | +// Output: 2 -> 1 -> 9. That is, 912. |
| 7 | +// |
| 8 | +// Follow up: Suppose the digits are stored in forward order. Repeat the above problem. |
| 9 | +// Example |
| 10 | +// Input: (6 -> 1 -> 7) + (2 -> 9 -> 5). That is, 617 + 295. |
| 11 | +// Output: 9 -> 1 -> 2. That is, 912. |
| 12 | + |
| 13 | +const assert = require("assert"); |
| 14 | +const { |
| 15 | + arrayToLinkedList, |
| 16 | + compareLinkedLists, |
| 17 | + LinkedListNode, |
| 18 | +} = require("../../lib/avc278/linkedlist"); |
| 19 | + |
| 20 | +/** |
| 21 | + * Sums two linked lists, where the linked lists are displayed in reverse order |
| 22 | + * @param {LinkedListNode} listOne input linked list 1 to be added |
| 23 | + * @param {LinkedListNode} listTwo input linked list 2 to be added |
| 24 | + * @return {LinkedListNode} ouput sum of input linked lists |
| 25 | + * |
| 26 | + * For this problem, we need to keep a carry in case two node values, when summed, are greater than 10. We also need to |
| 27 | + * keep track of the sum linked list head, as well as the previous and current nodes of the sum linked list. |
| 28 | + * We traverse through our two linked lists, adding their node values together, storing them into a temp sum, then |
| 29 | + * creating a new node based on that sum value, noting whether the sum is greater than 10 or not, and adding it on to |
| 30 | + * the sumHead linked list. Traversing through listOne requires a runtime of O(N) where N is the length of the list, |
| 31 | + * and similarly, traversing through listTwo requires a runtime of O(M) where M is the length of the list. Since they |
| 32 | + * travel at the same pace, this will require a runtime of of max(M, N). Since we need to construct an answer based on |
| 33 | + * the sum of the two lists, we will construct a linked list whose length is the maximum of M and N, plus 1, which |
| 34 | + * consequently also requires max(M, N) space. |
| 35 | + * Runtime: O(max(M, N)) |
| 36 | + * Space: O(max(M, N)) |
| 37 | + * |
| 38 | + */ |
| 39 | +const sumLists = (listOne, listTwo) => { |
| 40 | + let carry = 0; |
| 41 | + let sumHead; |
| 42 | + let prevNode; |
| 43 | + |
| 44 | + while (carry > 0 || listOne !== null || listTwo !== null) { |
| 45 | + let sum = carry; |
| 46 | + if (listOne !== null) { |
| 47 | + sum += listOne.val; |
| 48 | + listOne = listOne.next; |
| 49 | + } |
| 50 | + if (listTwo !== null) { |
| 51 | + sum += listTwo.val; |
| 52 | + listTwo = listTwo.next; |
| 53 | + } |
| 54 | + |
| 55 | + carry = 0; |
| 56 | + if (sum >= 10) { |
| 57 | + carry = 1; |
| 58 | + } |
| 59 | + const currNode = new LinkedListNode(sum % 10); |
| 60 | + if (!sumHead) { |
| 61 | + sumHead = currNode; |
| 62 | + } |
| 63 | + |
| 64 | + if (prevNode) prevNode.next = currNode; |
| 65 | + prevNode = currNode; |
| 66 | + } |
| 67 | + |
| 68 | + return sumHead; |
| 69 | +}; |
| 70 | + |
| 71 | +describe(module.filename, () => { |
| 72 | + it("should return the sum of two linked lists of the same length", () => { |
| 73 | + const arr1 = [7, 1, 6]; |
| 74 | + const ll1 = arrayToLinkedList(arr1); |
| 75 | + const arr2 = [5, 9, 2]; |
| 76 | + const ll2 = arrayToLinkedList(arr2); |
| 77 | + const expectedArrSum = [2, 1, 9]; |
| 78 | + const expectedListSum = arrayToLinkedList(expectedArrSum); |
| 79 | + |
| 80 | + const sum = sumLists(ll1, ll2); |
| 81 | + |
| 82 | + assert.ok(compareLinkedLists(sum, expectedListSum)); |
| 83 | + }); |
| 84 | + it("should return the sum of two linked lists of different lengths", () => { |
| 85 | + const arr1 = [0, 1]; |
| 86 | + const ll1 = arrayToLinkedList(arr1); |
| 87 | + const arr2 = [9, 9, 9]; |
| 88 | + const ll2 = arrayToLinkedList(arr2); |
| 89 | + const expectedArrSum = [9, 0, 0, 1]; |
| 90 | + const expectedListSum = arrayToLinkedList(expectedArrSum); |
| 91 | + |
| 92 | + const sum = sumLists(ll1, ll2); |
| 93 | + |
| 94 | + assert.ok(compareLinkedLists(sum, expectedListSum)); |
| 95 | + }); |
| 96 | + it("should return the sum of two linked lists when one is empty", () => { |
| 97 | + const arr1 = [0]; |
| 98 | + const ll1 = arrayToLinkedList(arr1); |
| 99 | + const arr2 = [9, 9, 9]; |
| 100 | + const ll2 = arrayToLinkedList(arr2); |
| 101 | + |
| 102 | + const sum = sumLists(ll1, ll2); |
| 103 | + |
| 104 | + assert.ok(compareLinkedLists(sum, ll2)); |
| 105 | + }); |
| 106 | +}); |
0 commit comments