Skip to content

Instantly share code, notes, and snippets.

@kavunshiva
Last active January 4, 2023 03:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kavunshiva/d642c9937b76ef31445ddd7c10f821cc to your computer and use it in GitHub Desktop.
Save kavunshiva/d642c9937b76ef31445ddd7c10f821cc to your computer and use it in GitHub Desktop.
no. 21: merge two sorted lists from l33tcode (with two approaches: arrays and linked lists)
class Node {
constructor(val=0, next=null) {
this.val = val;
this.next = next;
}
}
const generateLinkedList = (arr) => {
let current = new Node();
const head = current;
// the following one-liner is the same as:
// for (let i = 0; i < arr.length; i++) {
// current.next = new Node(arr[i]);
// current = current.next;
// }
for (val of arr) current = current.next = new Node(val);
return head.next;
};
const copyLinkedList = (linkedList, current = new Node(), head = null) => {
if (!linkedList) return head && head.next;
if (!head) head = current;
current = current.next = new Node(linkedList.val);
return copyLinkedList(linkedList.next, current, head);
};
// solve using arrays
const mergeArrays = (a1, a2) => {
// declare copies so as to preserve original arrays
let arr1 = [...a1];
let arr2 = [...a2];
const merged = [];
let lowerVal;
while (arr1.length || arr2.length) {
if (!arr1.length) return [...merged, ...arr2];
if (!arr2.length) return [...merged, ...arr1];
if (arr1[0] < arr2[0]) {
lowerVal = arr1.shift(); // removes and returns first element from array
} else {
lowerVal = arr2.shift();
}
merged.push(lowerVal)
}
return merged;
};
// solve using linked lists
const mergeLinkedLists = (l1, l2) => {
// declare copies so as to preserve original linked lists
let list1 = copyLinkedList(l1);
let list2 = copyLinkedList(l2);
let current = new Node();
const head = current;
while (list1 || list2) {
if (!list1) {
current.next = list2;
break;
} else if (!list2) {
current.next = list1;
break;
} else if (list1.val < list2.val) {
current = current.next = list1;
list1 = list1.next;
} else {
current = current.next = list2;
list2 = list2.next;
}
}
return head.next;
};
// ********** TEST DATA BELOW **********
// sample lists
const array1 = [1,1,1,11,728]; //[1, 3, 5, 7];
const array2 = [1,2,3,3,4,103,1002]; //[2, 4, 6];
// generate merged arrays
const mergedArrays = mergeArrays(array1, array2);
console.log('merged array values:');
for (val of mergedArrays) console.log(val); // print values from merged arrays
console.log(); // add a blank line
// generate merged linked list
const linkedList1 = generateLinkedList(array1);
const linkedList2 = generateLinkedList(array2);
const mergedLinkedLists = mergeLinkedLists(linkedList1, linkedList2);
// print values from merged linked lists
console.log('merged linked list values:');
let mergedCopy = mergedLinkedLists;
while (mergedCopy) {
console.log(mergedCopy.val);
mergedCopy = mergedCopy.next;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment