mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 16:26:47 +08:00
feat: add MergeTwoSortedLinkedLIsts algorithms (#1442)
* feat: add mergeTwoSortedLinkedLIsts algorithms * remove class and unnecessary function change the function params and return value from Node to LinkedList.
This commit is contained in:

committed by
GitHub

parent
3823eded0a
commit
ca761d87b6
45
Data-Structures/Linked-List/MergeTwoSortedLinkedLists.js
Normal file
45
Data-Structures/Linked-List/MergeTwoSortedLinkedLists.js
Normal file
@ -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 }
|
@ -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)
|
||||||
|
})
|
||||||
|
})
|
Reference in New Issue
Block a user