Skip to content

Instantly share code, notes, and snippets.

@dashsaurabh
Created January 3, 2020 11:49
Show Gist options
  • Save dashsaurabh/915a6ce2bce683928430eb2d9187bf32 to your computer and use it in GitHub Desktop.
Save dashsaurabh/915a6ce2bce683928430eb2d9187bf32 to your computer and use it in GitHub Desktop.
Merge Two Sorted Linked Lists
class Node {
constructor(val){
this.val = val;
this.next = null
}
}
class SinglyLinkedList{
constructor(){
this.length = 0;
this.head = null;
this.tail = null;
}
push(val){
let newNode = new Node(val)
if (this.length === 0){
this.head = newNode;
this.tail = newNode;
this.length++;
}else{
this.tail.next = newNode;
this.tail = newNode;
this.length++
}
return this;
}
pop(){
if(this.length === 0) return undefined;
let currentNode = this.head;
let prevNode = currentNode;
while(currentNode.next){
prevNode = currentNode;
currentNode=currentNode.next;
}
prevNode.next = null;
this.tail = prevNode;
this.length--;
return currentNode;
}
shift(){
if (this.length === 0) return undefined;
let currentHead = this.head;
this.head = this.head.next;
this.length--;
return currentHead;
}
unshift(val){
let newNode = new Node(val)
if(this.length === 0){
this.head = newNode;
this.tail = newNode;
}else{
newNode.next = this.head
this.head = newNode;
}
this.length++;
return this;
}
get(index){
if (this.length === 0) return undefined;
if (index >= this.length) return undefined;
let counter = 0;
let currentNode = this.head;
while(counter < index){
currentNode = currentNode.next;
counter++;
}
return currentNode;
}
set(index, val){
let node = this.get(index);
if(node){
node.val = val;
return node;
}
return undefined;
}
insert(index, val){
if(index < 0 || index > this.length) return false;
if(index === 0) return !!this.unshift(val);
if(index === this.length) return !!this.push(val)
let newNode = new Node(val);
let prevNode = this.get(index - 1);
let temp = prevNode.next;
prevNode.next = newNode
newNode.next = temp;
this.length++
return true;
}
remove(index){
if (index < 0 || index > this.length) return undefined;
if(index === 0) return this.shift();
if (index === this.length) return this.pop();
let foundNode = this.get(index - 1);
let toBeRemovedNode = foundNode.next;
foundNode.next = toBeRemovedNode.next;
toBeRemovedNode.next = null;
this.length --
return toBeRemovedNode;
}
traverse() {
let current = this.head;
while(current !== null) {
console.log(current.val)
current = current.next;
}
}
}
function mergeSortedList(node1, node2) {
let mergeSortedList = new SinglyLinkedList();
while(node1 !== null && node2 !== null) {
if(node1.val < node2.val) {
mergeSortedList.push(node1.val);
node1 = node1.next;
}else {
mergeSortedList.push(node2.val);
node2 = node2.next;
}
}
//move remaining elements
mergeSortedList.tail.next = node1 !== null ? node1 : node2
return mergeSortedList;
}
let list1 = new SinglyLinkedList();
let list2 = new SinglyLinkedList();
list1.push(1);
list2.push(2);
list1.push(3);
list2.push(4);
list1.push(5);
list2.push(6);
let newList = mergeSortedList(list1.head, list2.head);
newList.traverse();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment