This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Node{ | |
| private int data; | |
| private Node next; | |
| public Node( int data ){ | |
| this.data = data; | |
| this.next = null; | |
| } | |
| public void setNext( Node next ){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public void reverseKLL( Node head, int k ){ | |
| Node prev; | |
| Node next; | |
| Node current = head; | |
| int count = 0; | |
| while( current != null ){ | |
| Node boundaryNode = getBoundaryNode( current ); | |
| while( boundaryNode != null && count < k ){ | |
| next = current.getNext(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Method 1 : Time = O(n), Space = O(1) | |
| public List<Pair<Integer, Integer>> getPairsOfIntegers( int[] numbers, int sum ){ | |
| List<Pair<Integer,Integer>> results = new ArrayList<>(); | |
| int[] sortedNumbers = doRadixSort( numbers ); | |
| int left = 0; | |
| int right = sortedNumbers.length - 1; | |
| while( left < right ){ | |
| int currentSum = sortedNumbers[ left ] + sortedNumbers[ right ]; | |
| if ( currentSum == sum ){ | |
| results.add( new Pair<Integer,Integer>( sortedNumbers[ left ], sortedNumbers[ right ] ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public String removeVowels( String testString ){ | |
| char[] s = testString.toCharArray(); | |
| int vowelCount = 0; | |
| int vowelStartIndex = -1; | |
| for ( int i = 0; i < s.length; i ++ ){ | |
| if( isVowel( s[i] ) ){ | |
| if( vowelCount == 0 ) | |
| vowelStartIndex = i; | |
| vowelCount++; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public void quickSort(int[] array) { | |
| quickSort(array, 0, array.length - 1); | |
| } | |
| public void quickSort(int[] array, int left, int right) { | |
| int pivotIndex = partition(array, left, right); | |
| if (left < pivotIndex - 1) { | |
| quickSort(array, left, pivotIndex - 1); | |
| } | |
| if (right > pivotIndex) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public void sort( int[] numbers ){ | |
| mergesort( numbers ); | |
| } | |
| public void mergesort( int[] numbers ){ | |
| if( numbers.length > 1 ){ | |
| int[] leftHalf = splitLeft( numbers ); | |
| int[] rightHalf = splitRight( numbers ); | |
| mergesort( leftHalf ); | |
| mergesort( rightHalf ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public boolean isSumExists( Node root, int sum ){ | |
| if( root == null && sum != 0 ){ | |
| return false; | |
| } | |
| int currentNodeSum = sum - root.getData(); | |
| if( currentNodeSum == 0 ){ | |
| return true; | |
| } else{ | |
| return ( isSumExists( root.getLeftChild(), currentNodeSum ) || isSumExists( root.getRightChild(), currentNodeSum ) ); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public int height( Node root ){ | |
| if( root == null ){ | |
| return 0; | |
| } | |
| int heightLeft = height( root.getLeftChild() ); | |
| int heightRight = height( root.getRightChild() ); | |
| return Math.max( heightLeft, heightRight ) + 1; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public Node getLCA( Node node, int a, int b ){ | |
| if( node == null ){ | |
| return node; | |
| } | |
| if( node.getData() == a || node.getData() == b ){ | |
| return node; | |
| } | |
| Node lcaInLeft = getLCA( node.getLeft(), a, b ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Method 1: | |
| public Node getClosestBSTNode( Node root, int k ){ | |
| if( root == null ){ | |
| return null; | |
| } | |
| return getClosestBSTNode( root, k, root.data() - k ); | |
| } | |
| public Node getClosestBSTNode( Node node, int k, int parentDiff ){ |
OlderNewer