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
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* Partition: Write code to partition a linked list around a value x, | |
* such that all nodes less than x come before all nodes greater than | |
* or equal to x. lf x is contained within the list, the values of x only need | |
* to be after the elements less than x (see below).The partition element x can | |
* appear anywhere in the "right partition"; it does not need to appear between the left and right partitions. | |
* EXAMPLE |
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
/** | |
* Delete Middle Node: Implement an algorithm to delete a node in the middle | |
* (i.e., any node but the first and last node, not necessarily the exact middle) | |
* of a singly linked list, given only access to that node. | |
* EXAMPLE | |
* Input: the node c from the linked list a -> b -> c -> d -> e -> f | |
* Result: nothing is returned, but the new linked list looks like a -> b -> d -> e -> f | |
* Hints: #72 | |
*/ | |
public class DeleteMiddleNode { |
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
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* Return Kth to Last: Implement an algorithm to find the kth to last element of a singly linked list. | |
* Hints: #8, #25, #47, #67, # 726 | |
*/ | |
public class ReturnKthToLast { | |
public static void main(String[] args) { | |
Node<Integer> node1 = new Node<>(1, 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
import java.util.LinkedList; | |
import java.util.List; | |
/** | |
* Remove Dups: | |
* Write code to remove duplicates from an unsorted linked list. | |
* <p> | |
* FOLLOW UP | |
* How would you solve this problem if a temporary buffer is not allowed? |
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
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
/** | |
* String Rotation: Assume you have a method isSubstring which | |
* checks if one word is a substring of another. | |
* Given two strings, s1 and s2, write code to check if s2 is a rotation of s1 | |
* using only one call to isSubstring | |
* (e.g.,"waterbottle" is a rotation of "erbottlewat"). |
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
import java.util.*; | |
/** | |
* Zero Matrix: Write an algorithm such that if an element in an MxN matrix is 0, | |
* its entire row and column are set to O. | |
* Hints: #17, #74, #102 | |
*/ | |
public class ZeroMatrix { | |
public static void main(String[] args) { | |
int[][] matrix = new int[][]{ |
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
import java.util.HashMap; | |
import java.util.Map; | |
/** | |
* String Compression: Implement a method to perform basic string compression | |
* using the counts of repeated characters. For example, | |
* the string aabcccccaaa would become a2b1c5a3. If the "compressed" | |
* string would not become smaller than the original string, your method | |
* should return the original string. You can assume the string has only uppercase and lowercase letters (a - z). |
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
/** | |
* URLify: Write a method to replace all spaces in a string with '%20: | |
* You may assume that the string has sufficient space at the end to hold | |
* the additional characters, and that you are given the "true" length of the string. | |
* (Note: If implementing in Java, please use a character array so that you can perform this operation in place.) | |
* EXAMPLE | |
* Input: "Mr John Smith " J 13 Output: "Mr%20John%20Smith" Hints: #53, #7 78 | |
*/ | |
public class URLify { |
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
import java.util.Arrays; | |
/** | |
Is Unique: Implement an algorithm to determine if a string has all unique characters. | |
What if you cannot use additional data structures? | |
Hints: #44, # 777, # 732 | |
*/ | |
public class IsUnique { | |
public static void main(String[] args) { |
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
import java.util.Arrays; | |
/** | |
Check Permutation: Given two strings, write a method to decide if one is a permutation of the other. | |
Hints: #7, #84, #722, #737 | |
*/ | |
public class CheckPermutation { | |
public static void main(String[] args) { | |
String pattern = "abbc"; | |
String sequence = "cbabadcbbabbcbabaabccbabc"; |