Skip to content

Instantly share code, notes, and snippets.

View rahul4coding's full-sized avatar
🏠
Working from home

Rahul Bhatija rahul4coding

🏠
Working from home
View GitHub Profile
@rahul4coding
rahul4coding / SparseArray.js
Last active January 24, 2023 18:21
Sparse Arrays Hackerrank Solution by Rahul Bhatija
// https://www.hackerrank.com/challenges/sparse-arrays
// Simple JavaScript solution
function matchingStrings(strings, queries) {
return queries.map(x=>strings.filter(y=>y===x).length)
}
@rahul4coding
rahul4coding / insertNodeAtTail.js
Created December 16, 2019 16:55
Insert node at a tail of Linked List | HAckerRank
// https://www.hackerrank.com/challenges/insert-a-node-at-the-tail-of-a-linked-list/problem
function insertNodeAtTail(head, data) {
var newNode = new SinglyLinkedListNode(data);
if(head===null){
head = newNode;
return head;
}else{
@rahul4coding
rahul4coding / insertNodeAtHead.js
Created December 16, 2019 17:05
Insert a Node at the head of a Linked List | JavaScript
//https://www.hackerrank.com/challenges/insert-a-node-at-the-head-of-a-linked-list/problem?h_r=next-challenge&h_v=zen
function insertNodeAtHead(head, data) {
var newNode = new SinglyLinkedListNode(data);
if(head==null){
head = newNode;
return head;
}else{
@rahul4coding
rahul4coding / insertNodeAtPosition.js
Last active October 21, 2022 05:50
Insert node at specified position in a Linked List | JS
function insertNodeAtPosition(head, data, position) {
var trackedNode = head;
var newNode = new SinglyLinkedListNode(data);
if(head==null){
head = newNode
return head;
}
@rahul4coding
rahul4coding / deleteNode.js
Created December 16, 2019 18:19
Delete node in a Linked List from a specified position
function deleteNode(head, position) {
if(position==0){
return head.next
}
head.next = deleteNode(head.next,position-1)
return head;
}
@rahul4coding
rahul4coding / reversePrint.js
Created December 16, 2019 18:31
Reverse Print a given linkedList | JS
function reversePrint(head) {
//recursion is king
if(head!==null){
reversePrint(head.next);
return console.log(head.data);
}
}
@rahul4coding
rahul4coding / CompareLists.js
Created December 16, 2019 19:05
Comparing Two Linked Lists | JS
function CompareLists(a, b) {
while(a!==null && b!==null){
if(a.data !== b.data){
return 0
}else{
a=a.next;
b=b.next;
}
@rahul4coding
rahul4coding / mergeLists.js
Created December 16, 2019 19:24
merge two sorted Linked list
function mergeLists(head1, head2) {
var result = new SinglyLinkedListNode();
// case1
if(head1==null){
return head2;
}else if(head2==null){
return head1;
}
@rahul4coding
rahul4coding / getNode.js
Created December 16, 2019 19:49
Get node value in a linked list position from tail | JS
function getNode(head, positionFromTail) {
var length =0;
var tp = head;
while(tp!==null){
length++;
tp=tp.next
}
let currentPosition=0;
if(head==null){
@rahul4coding
rahul4coding / findMergeNode.js
Created December 17, 2019 21:28
Find merge point of two linked list | JS
function findMergeNode(headA, headB) {
let currentA = headA;
let currentB = headB;
while(currentA!==currentB){
//headA
if(currentA.next==null){
currentA.next=headB
}else{