|
| 1 | +// Remove Dups: Write code to remove duplicates from an unsorted linked list. |
| 2 | +// Follow Up: How would you solve this problem if a temporary buffer is not allowed? |
| 3 | + |
| 4 | +const assert = require("assert"); |
| 5 | + |
| 6 | +class LinkedListNode { |
| 7 | + constructor(val, next) { |
| 8 | + this.val = val === undefined ? null : val; |
| 9 | + this.next = next === undefined ? null : next; |
| 10 | + } |
| 11 | +} |
| 12 | + |
| 13 | +const arrayToLinkedList = (arr) => { |
| 14 | + let tail = null; |
| 15 | + for (let i = arr.length - 1; i >= 0; i--) { |
| 16 | + tail = new LinkedListNode(arr[i], tail); |
| 17 | + } |
| 18 | + return tail; |
| 19 | +}; |
| 20 | + |
| 21 | +const compareLinkedLists = (A, B) => { |
| 22 | + if (!A && !B) return true; |
| 23 | + |
| 24 | + while (A !== null && B !== null) { |
| 25 | + if (A.val !== B.val) return false; |
| 26 | + A = A.next; |
| 27 | + B = B.next; |
| 28 | + } |
| 29 | + |
| 30 | + return !((A && !B) || (!A && B)); |
| 31 | +}; |
| 32 | + |
| 33 | +/** |
| 34 | + * Removes duplicates from a linked list |
| 35 | + * @param {LinkedListNode} list input linked list to mutate |
| 36 | + * @return {null} |
| 37 | + * |
| 38 | + * Keep a temporary set for storing values we pass through, of maximum size N where N is the number of nodes in the |
| 39 | + * input linked list in the worst case. This requires an additional O(N) space. We store two pointers as we traverse |
| 40 | + * through the linked list, skipping nodes with values already in our set. |
| 41 | + * Runtime: O(N) |
| 42 | + * Space: O(N) |
| 43 | + * |
| 44 | + */ |
| 45 | +const removeDups1 = (list) => { |
| 46 | + if (!list) return; |
| 47 | + const seen = new Set(); |
| 48 | + let currNode = list; |
| 49 | + seen.add(currNode.val); |
| 50 | + for (let nextNode = currNode.next; nextNode.next !== null; nextNode = nextNode.next) { |
| 51 | + if (seen.has(nextNode.val)) continue; |
| 52 | + |
| 53 | + seen.add(nextNode.val); |
| 54 | + currNode.next = nextNode; |
| 55 | + currNode = currNode.next; |
| 56 | + } |
| 57 | + |
| 58 | + currNode.next = null; |
| 59 | +}; |
| 60 | + |
| 61 | +/** |
| 62 | + * Removes duplicates from a linked list |
| 63 | + * @param {LinkedListNode} list input linked list to mutate |
| 64 | + * @return {null} |
| 65 | + * |
| 66 | + * Instead of keeping a temporary set, we need another way to remove all future dupes of the current node. |
| 67 | + * One way of doing this, is to skip all future nodes containing the same val as the current one. |
| 68 | + * This nest loop through the linked list increases our run time to O(N^2), but since we don't use any additional space, |
| 69 | + * we reduced the space complexity to O(1). |
| 70 | + * |
| 71 | + * Runtime: O(N^2) |
| 72 | + * Space: O(1) |
| 73 | + * |
| 74 | + */ |
| 75 | +const removeDups2 = (list) => { |
| 76 | + if (!list) return; |
| 77 | + let currNode = list; |
| 78 | + while (currNode !== null) { |
| 79 | + let nextNode = currNode; |
| 80 | + while (nextNode.next !== null) { |
| 81 | + if (currNode.val === nextNode.next.val) { |
| 82 | + nextNode.next = nextNode.next.next; |
| 83 | + } else { |
| 84 | + nextNode = nextNode.next; |
| 85 | + } |
| 86 | + } |
| 87 | + currNode = currNode.next; |
| 88 | + } |
| 89 | +}; |
| 90 | + |
| 91 | +const removeDups = [removeDups1, removeDups2]; |
| 92 | +removeDups.forEach((removeDup) => { |
| 93 | + describe(removeDup.name, () => { |
| 94 | + it("should return the linked list without duplicates", () => { |
| 95 | + const arr = [2, 2, 3, 4, 2, 4]; |
| 96 | + let ll1 = arrayToLinkedList(arr); |
| 97 | + const expectedArr = [2, 3, 4]; |
| 98 | + let expectedLL1 = arrayToLinkedList(expectedArr); |
| 99 | + |
| 100 | + removeDup(ll1); |
| 101 | + assert.ok(compareLinkedLists(ll1, expectedLL1)); |
| 102 | + }); |
| 103 | + it("should return an empty linked list", () => { |
| 104 | + const arr = []; |
| 105 | + let ll1 = arrayToLinkedList(arr); |
| 106 | + const expectedArr = []; |
| 107 | + let expectedLL1 = arrayToLinkedList(expectedArr); |
| 108 | + |
| 109 | + removeDup(ll1); |
| 110 | + assert.ok(compareLinkedLists(ll1, expectedLL1)); |
| 111 | + }); |
| 112 | + it("should return false when comparing unequal linked lists", () => { |
| 113 | + assert.ok( |
| 114 | + !compareLinkedLists(arrayToLinkedList([]), arrayToLinkedList(["hi!"])) |
| 115 | + ); |
| 116 | + }); |
| 117 | + it("should return false when comparing another pair of unequal linked lists", () => { |
| 118 | + assert.ok( |
| 119 | + !compareLinkedLists(arrayToLinkedList([1, 2]), arrayToLinkedList([1])) |
| 120 | + ); |
| 121 | + }); |
| 122 | + }); |
| 123 | +}); |
0 commit comments