Skip to content

feat: add MergeTwoSortedLinkedLIsts algorithms #1442

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions Data-Structures/Linked-List/MergeTwoSortedLinkedLists.js
Original file line number Diff line number Diff line change
@@ -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 }
Original file line number Diff line number Diff line change
@@ -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)
})
})