|
| 1 | +// Partition: Write code to partition a linked list around a value x, such that all nodes less than x come before all |
| 2 | +// nodes greater than or equal to x. If x is contained within the list, the values of x only need to be after the |
| 3 | +// elements less than x (see below). The partition element x can appear anywhere in the "right partition"; it does not |
| 4 | +// need to appear between the left and right partitions. |
| 5 | +// EXAMPLE |
| 6 | +// Input: 3 -> 5 -> 8 -> 5 -> 10 -> 2 -> 1[partition=5] |
| 7 | +// Output: 3 -> 1 -> 2 -> 10 -> 5 -> 5 -> 8 |
| 8 | + |
| 9 | +const assert = require("assert"); |
| 10 | +const { |
| 11 | + arrayToLinkedList, |
| 12 | + compareLinkedLists, |
| 13 | +} = require("../../lib/avc278/linkedlist"); |
| 14 | + |
| 15 | +/** |
| 16 | + * A destructive function, `partition`, splits a linked list based on the input `partitionValue`, with lesser values to |
| 17 | + * the left and greater values to the right, and returns the partitioned linked list. |
| 18 | + * @param {LinkedListNode} head input linked list to be partitioned |
| 19 | + * @return {LinkedListNode} partitioned linked list |
| 20 | + * |
| 21 | + * For this problem, we need to store pointers to four linked list nodes: |
| 22 | + * `lessHead` - pointing to the head of the linked list containing values less than the partition value |
| 23 | + * `lessTail` - pointing to the tail of the linked list containing values less than the partition value |
| 24 | + * `moreHead` - pointing to the head of the linked list containing values more than the partition value |
| 25 | + * `moreTail` - pointing to the tail of the linked list containing values more than the partition value |
| 26 | + * Iterating once through the original linked list makes it so the runtime is O(N), where N is the length of the input |
| 27 | + * linked list. Since we only store four pointers to nodes at one time, our additional space required is O(1). |
| 28 | + * Runtime: O(N) |
| 29 | + * Space: O(1) |
| 30 | + * |
| 31 | + */ |
| 32 | +const partition = (head, partitionValue) => { |
| 33 | + let lessHead; |
| 34 | + let lessTail; |
| 35 | + let moreHead; |
| 36 | + let moreTail; |
| 37 | + |
| 38 | + while (head !== null) { |
| 39 | + const next = head.next; |
| 40 | + if (head.val < partitionValue) { |
| 41 | + if (!lessTail) { |
| 42 | + lessTail = head; |
| 43 | + lessHead = head; |
| 44 | + } else { |
| 45 | + lessTail.next = head; |
| 46 | + lessTail = lessTail.next; |
| 47 | + } |
| 48 | + } else { |
| 49 | + if (!moreTail) { |
| 50 | + moreTail = head; |
| 51 | + moreHead = head; |
| 52 | + } else { |
| 53 | + moreTail.next = head; |
| 54 | + moreTail = moreTail.next; |
| 55 | + } |
| 56 | + } |
| 57 | + head = next; |
| 58 | + } |
| 59 | + |
| 60 | + if (!lessHead && moreHead) { |
| 61 | + return moreHead; |
| 62 | + } |
| 63 | + |
| 64 | + if (lessHead && !moreHead) { |
| 65 | + return lessHead; |
| 66 | + } |
| 67 | + |
| 68 | + lessTail.next = moreHead; |
| 69 | + moreTail.next = null; |
| 70 | + return lessHead; |
| 71 | +}; |
| 72 | + |
| 73 | +describe(module.filename, () => { |
| 74 | + it("should return the partitioned linked list when the partition value exists in the linked list", () => { |
| 75 | + const arr = [3, 5, 8, 5, 10, 2, 1]; |
| 76 | + const ll = arrayToLinkedList(arr); |
| 77 | + const expectedArr = [3, 2, 1, 5, 8, 5, 10]; |
| 78 | + const expectedPartition = arrayToLinkedList(expectedArr); |
| 79 | + |
| 80 | + const partitionedLL = partition(ll, 5); |
| 81 | + |
| 82 | + assert.ok(compareLinkedLists(partitionedLL, expectedPartition)); |
| 83 | + }); |
| 84 | + it("should return the partitioned linked list when the partition value does not exist in the linked list", () => { |
| 85 | + const arr = [1, 9, 3, 8, 6, 7, 4, 2, 10]; |
| 86 | + const ll = arrayToLinkedList(arr); |
| 87 | + const expectedArr = [1, 3, 4, 2, 9, 8, 6, 7, 10]; |
| 88 | + const expectedPartition = arrayToLinkedList(expectedArr); |
| 89 | + |
| 90 | + const partitionedLL = partition(ll, 5); |
| 91 | + |
| 92 | + assert.ok(compareLinkedLists(partitionedLL, expectedPartition)); |
| 93 | + }); |
| 94 | + it("should return the partitioned linked list when the partition flips the array", () => { |
| 95 | + const arr = [10, 1]; |
| 96 | + const ll = arrayToLinkedList(arr); |
| 97 | + const expectedArr = [1, 10]; |
| 98 | + const expectedPartition = arrayToLinkedList(expectedArr); |
| 99 | + |
| 100 | + const partitionedLL = partition(ll, 5); |
| 101 | + |
| 102 | + assert.ok(compareLinkedLists(partitionedLL, expectedPartition)); |
| 103 | + }); |
| 104 | + it("should return the partitioned linked list when the partition value is less than all values in the array", () => { |
| 105 | + const arr = [10]; |
| 106 | + const ll = arrayToLinkedList(arr); |
| 107 | + const expectedArr = [10]; |
| 108 | + const expectedPartition = arrayToLinkedList(expectedArr); |
| 109 | + |
| 110 | + const partitionedLL = partition(ll, 5); |
| 111 | + |
| 112 | + assert.ok(compareLinkedLists(partitionedLL, expectedPartition)); |
| 113 | + }); |
| 114 | + it("should return the partitioned linked list when the partition value is more than all values in the array", () => { |
| 115 | + const arr = [1]; |
| 116 | + const ll = arrayToLinkedList(arr); |
| 117 | + const expectedArr = [1]; |
| 118 | + const expectedPartition = arrayToLinkedList(expectedArr); |
| 119 | + |
| 120 | + const partitionedLL = partition(ll, 5); |
| 121 | + |
| 122 | + assert.ok(compareLinkedLists(partitionedLL, expectedPartition)); |
| 123 | + }); |
| 124 | +}); |
0 commit comments