diff --git a/Data-Structures/Linked-List/MergeTwoSortedLinkedLists.js b/Data-Structures/Linked-List/MergeTwoSortedLinkedLists.js new file mode 100644 index 0000000000..df11c0ac2c --- /dev/null +++ b/Data-Structures/Linked-List/MergeTwoSortedLinkedLists.js @@ -0,0 +1,45 @@ +import { LinkedList } from './SinglyLinkedList.js' +/** + * A LinkedList-based solution for merging two sorted linked lists into one sorted list. + * + * @param {LinkedList} list1 - The the first sorted linked list. + * @param {LinkedList} list2 - The second sorted linked list. + * @returns {LinkedList} - The merged sorted linked list. + * + * @example + * const list1 = new LinkedList([1,2,4]); + * + * const list2 = new LinkedList([1,3,4]); + * + * const result = mergeLinkedLists(list1, list2); + * // Returns the merged linked list representing 1 -> 1 -> 2 -> 3 -> 4 -> 4 + */ + +function mergeLinkedLists(list1, list2) { + const mergedList = new LinkedList() + + let current1 = list1.headNode + let current2 = list2.headNode + + while (current1 || current2) { + if (!current1) { + mergedList.addLast(current2.data) + current2 = current2.next + } else if (!current2) { + mergedList.addLast(current1.data) + current1 = current1.next + } else { + if (current1.data < current2.data) { + mergedList.addLast(current1.data) + current1 = current1.next + } else { + mergedList.addLast(current2.data) + current2 = current2.next + } + } + } + + return mergedList +} + +export { mergeLinkedLists } diff --git a/Data-Structures/Linked-List/test/MergeTwoSortedLinkedLists.test.js b/Data-Structures/Linked-List/test/MergeTwoSortedLinkedLists.test.js new file mode 100644 index 0000000000..bc5bea953e --- /dev/null +++ b/Data-Structures/Linked-List/test/MergeTwoSortedLinkedLists.test.js @@ -0,0 +1,39 @@ +import { expect } from 'vitest' +import { mergeLinkedLists } from '../MergeTwoSortedLinkedLists.js' +import { LinkedList } from '../SinglyLinkedList.js' + +describe('MergeTwoSortedLinkedLists', () => { + it('Merges two sorted linked lists', () => { + const list1 = new LinkedList([1, 2, 4]) + + const list2 = new LinkedList([1, 3, 4]) + + const expectedResult = new LinkedList([1, 1, 2, 3, 4, 4]) + + const result = mergeLinkedLists(list1, list2) + + expect(result).toEqual(expectedResult) + }) + + it('Merges two empty linked lists', () => { + const list1 = new LinkedList() + const list2 = new LinkedList() + + const expectedResult = new LinkedList() + + const result = mergeLinkedLists(list1, list2) + + expect(result).toEqual(expectedResult) + }) + + it('Merges one empty linked list with a non-empty one', () => { + const list1 = new LinkedList() + const list2 = new LinkedList([1]) + + const expectedResult = new LinkedList([1]) + + const result = mergeLinkedLists(list1, list2) + + expect(result).toEqual(expectedResult) + }) +})