|
| 1 | +import { |
| 2 | + createSinglyLinkedListNode, |
| 3 | + getNthNode |
| 4 | +} from "../testUtilities/LinkedList"; |
| 5 | +import { SinglyLinkedListNode } from "../types/LinkedList"; |
| 6 | +import getIntersectionNode from "./intersectionOfTwoLinkedLists"; |
| 7 | + |
| 8 | +describe("160. Intersection of Two Linked Lists", () => { |
| 9 | + const TEST_CASES: [any, number[], number[], number, number][] = [ |
| 10 | + [8, [4, 1, 8, 4, 5], [5, 0, 1, 8, 4, 5], 2, 3], |
| 11 | + [2, [0, 9, 1, 2, 4], [3, 2, 4], 3, 1], |
| 12 | + [0, [2, 6, 4], [1, 5], 3, 2] |
| 13 | + ]; |
| 14 | + |
| 15 | + for (const [, valuesA, valuesB, skipA, skipB] of TEST_CASES) { |
| 16 | + const headA = createSinglyLinkedListNode(valuesA); |
| 17 | + const headB = createSinglyLinkedListNode(valuesB); |
| 18 | + let intersection: SinglyLinkedListNode<number> | null = null; |
| 19 | + |
| 20 | + if (skipA < valuesA.length) { |
| 21 | + intersection = getNthNode(headA!, skipA); |
| 22 | + |
| 23 | + getNthNode(headB!, skipB - 1).next = intersection; |
| 24 | + } |
| 25 | + |
| 26 | + it(`returns the ${skipA}th node of "linked list A" when called with [${valuesA}], [${valuesB}], ${skipA} and ${skipB}`, () => { |
| 27 | + expect(getIntersectionNode(headA, headB)).toBe(intersection); |
| 28 | + }); |
| 29 | + } |
| 30 | +}); |
0 commit comments