This file contains 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
... | |
// convert String to array of chars | |
char[] s = str.toCharArray(); | |
// get length of string | |
int n = s.length; | |
// get middle of string | |
int middle = n/2; |
This file contains 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
... | |
// create a stack of characters | |
Stack<Character> st = new Stack<Character>(); | |
// convert string to reverse into an array of characters | |
char[] arr = str.toCharArray(); | |
// iterate chars array and push each char into the stack | |
for(char c : arr){ |
This file contains 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
... | |
static boolean isAnagram(String str1, String str2){ | |
// not the same size its not anagram | |
if(str1.length() != str2.length()) return false; | |
// integer array of 26 for each letter of English alphabet | |
int[] letters = new int[26]; |
This file contains 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 | |
// create array of already known numbers | |
int[] numbers = {10,2,5,8,37,6}; | |
// printing all numbers | |
for(int i = 0; i < numbers.length; i++){ | |
System.out.println(numbers[i]); |
This file contains 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 class LinkedList<T> { | |
// Reference to the head node | |
Node head; | |
public boolean hasCycle(){ | |
// check if list empty | |
if(head == null) return false; | |
This file contains 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 class LinkedList<T> { | |
// Reference to the head node | |
Node head; | |
public void removeDuplicates(){ | |
// check if list empty | |
if(head == null) return; | |
This file contains 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 class LinkedList<T> { | |
// Reference to the head node | |
Node head; | |
public void removeNode(T data){ | |
// check if list empty | |
if(head == null) return; | |
This file contains 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 class LinkedList<T> { | |
// Reference to the head node | |
Node head; | |
public void addAfter(T data, T target){ | |
// create new node | |
Node newNode = new Node(data, null); | |
This file contains 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 class LinkedList<T> { | |
// Reference to the head node | |
Node head; | |
public void addToTail(T data){ | |
// create new node | |
Node newNode = new Node(data, null); | |
This file contains 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 class LinkedList<T> { | |
// Reference to the head node | |
Node head; | |
public void addToHead(T data){ | |
// create the new Node and add head as its next | |
Node newNode = new Node(data, head); | |