Home
Gaurav's GitHub Page
Cancel

Merge Sort Linked List

PROBLEM DESCRIPTION Merge Sort a given Linked List. SOLUTION public static Node mergeSort(Node h1){ if(h1 == null || h1.next == null) return h1; Node c1 = getCenter(h1); Node h2 ...

Merge Two Sorted Linked List - 2

PROBLEM DESCRIPTION Merge two sorted Linked List. leetcode SOLUTION public static Node mergeSortedLinkedList(Node h1, Node h2){ //If any of the list is empty, return the other sorted lis...

Reverse a Linked List in K Groups

PROBLEM DESCRIPTION Given a singly linked list and an integer k, reverse the nodes of the list k at a time and return the modified linked list. If there are less than k size, reverse them in the o...

Reverse a Linked List

PROBLEM DESCRIPTION Reverse the LinkedList. SOLUTION public static Node reverseLinkedList(Node head){ Node h1=head; Node t=head; Node h2=null; while(h1 != null){ h1 =...

Reverse First K Nodes - LinkedList

PROBLEM DESCRIPTION Reverse the first K Nodes of the LinkedList. SOLUTION public static Node reverseFirstKNodes(Node head, int k){ Node h1=head; Node t=head; Node h2=null; Nod...

Rabin Karp

Problem Description Check if pattern p exists in string s. Rabin Karp Solution package com.gauk; public class RabinKarp { private static final int M = 1000000007; private static fina...

Pattern Matcher

Problem Description Check if pattern p exists in string s. Solution public boolean checkForPattern(String s, String p){ String str = p + "$" + s; int[] lps = LPS.getLPS(str); for(...

Longest Prefix Suffix

Problem Description Given a string of characters, find the length of the longest proper prefix which is also a proper suffix. NOTE: Prefix and suffix can be overlapping but they should not be equa...

Common Elements in Two Arrays

Problem Description Given two integer arrays, A and B of size N and M, respectively. Your task is to find all the common elements in both the array. Each element in the result should appear as man...

Count Rectangles

Problem Description Given two arrays of integers A and B of size N each, where each pair (A[i], B[i]) for 0 <= i < N represents a unique point (x, y) in a 2-D Cartesian plane. Find and retu...