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 1 commit
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
64 changes: 64 additions & 0 deletions Data-Structures/Linked-List/MergeTwoSortedLinkedLists.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { Node } from './SinglyLinkedList.js'
/**
* A LinkedList-based solution for merging two sorted linked lists into one sorted list.
*
* @param {Node} list1 - The head of the first sorted linked list.
* @param {Node} list2 - The head of the second sorted linked list.
* @returns {Node} - The head of the merged sorted linked list.
*
* @example
* const list1 = new ListNode(1);
* list1.next = new ListNode(2);
* list1.next.next = new ListNode(4);
*
* const list2 = new ListNode(1);
* list2.next = new ListNode(3);
* list2.next.next = new ListNode(4);
*
* const result = mergeLists(list1, list2);
* // Returns the head of a linked list representing 1 -> 1 -> 2 -> 3 -> 4 -> 4
*/

class MergeTwoSortedLinkedLists {
mergeLists(list1, list2) {
let dummy = new Node(-1)
let current = dummy

while (list1 && list2) {
if (list1.val < list2.val) {
current.next = list1
list1 = list1.next
} else {
current.next = list2
list2 = list2.next
}
current = current.next
}

// If one of the lists is not empty, append it to the result
if (list1) {
current.next = list1
} else if (list2) {
current.next = list2
}

return dummy.next
}

/**
* Converts a linked list to an array.
*
* @param {Node} head - The head of the linked list.
* @returns {number[]} - An array representing the linked list values.
*/
linkedListToArray(head) {
const resultArray = []
while (head) {
resultArray.push(head.val)
head = head.next
}
return resultArray
}
}

export { MergeTwoSortedLinkedLists }
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { MergeTwoSortedLinkedLists } from '../MergeTwoSortedLinkedLists.js'
import { Node } from '../SinglyLinkedList.js'

describe('MergeTwoSortedLinkedLists', () => {
it('Merges two sorted linked lists', () => {
const list1 = new Node(1)
list1.next = new Node(2)
list1.next.next = new Node(4)

const list2 = new Node(1)
list2.next = new Node(3)
list2.next.next = new Node(4)

const expectedResult = new Node(1)
expectedResult.next = new Node(1)
expectedResult.next.next = new Node(2)
expectedResult.next.next.next = new Node(3)
expectedResult.next.next.next.next = new Node(4)
expectedResult.next.next.next.next.next = new Node(4)

const merger = new MergeTwoSortedLinkedLists()
const result = merger.mergeLists(list1, list2)

expect(merger.linkedListToArray(result)).toEqual(
merger.linkedListToArray(expectedResult)
)
})

it('Merges two empty linked lists', () => {
const list1 = null
const list2 = null

const expectedResult = null

const merger = new MergeTwoSortedLinkedLists()
const result = merger.mergeLists(list1, list2)

expect(result).toEqual(expectedResult)
})

it('Merges one empty linked list with a non-empty one', () => {
const list1 = null
const list2 = new Node(0)

const expectedResult = new Node(0)

const merger = new MergeTwoSortedLinkedLists()
const result = merger.mergeLists(list1, list2)

expect(merger.linkedListToArray(result)).toEqual(
merger.linkedListToArray(expectedResult)
)
})
})