Home
Gaurav's GitHub Page
Cancel

Clone Linked List with Random Pointer

PROBLEM DESCRIPTION A linked list of length n is given such that each node contains an additional random pointer, which could point to any node in the list, or null. Construct a deep copy of the l...

Merge K Sorted Linked Lists

PROBLEM DESCRIPTION You are given the head of K sorted Linked Lists. Every Node is defined as: class Node{ int data; Node right; Node down; Node(int x){ data=x; r...

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...