|
| 1 | +package utils; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | +import java.util.Random; |
| 6 | + |
| 7 | +import classes.ListNode; |
| 8 | + |
| 9 | +public class CommonUtils { |
| 10 | + |
| 11 | + private static final int DEFAULT_TREE_SIZE = 10; |
| 12 | + private static final int DEFAULT_UPPER_BOUND = 100; |
| 13 | + |
| 14 | + public static void print(String message) { |
| 15 | + System.out.print(message); |
| 16 | + } |
| 17 | + |
| 18 | + public static void print(int num) { |
| 19 | + System.out.print(num); |
| 20 | + } |
| 21 | + |
| 22 | + public static void println(String message) { |
| 23 | + System.out.println(message); |
| 24 | + } |
| 25 | + |
| 26 | + public static void println(int num) { |
| 27 | + System.out.println(num); |
| 28 | + } |
| 29 | + |
| 30 | + public static void println() { |
| 31 | + System.out.println(); |
| 32 | + } |
| 33 | + |
| 34 | + //overloaded method to take only one argument |
| 35 | + public static List<Integer> randomIntArrayGenerator(int size) { |
| 36 | + return CommonUtils.randomIntArrayGenerator(size, DEFAULT_UPPER_BOUND); |
| 37 | + } |
| 38 | + |
| 39 | + //overloaded method to take no argument |
| 40 | + public static List<Integer> randomIntArrayGenerator() { |
| 41 | + return CommonUtils.randomIntArrayGenerator(CommonUtils.DEFAULT_TREE_SIZE, DEFAULT_UPPER_BOUND); |
| 42 | + } |
| 43 | + |
| 44 | + //this one has two other overloaded methods as above |
| 45 | + public static List<Integer> randomIntArrayGenerator(int size, int upperBound) { |
| 46 | + List<Integer> result = new ArrayList<Integer>(); |
| 47 | + Random random = new Random(); |
| 48 | + for (int i = 0; i < size; i++) { |
| 49 | + int randomInt = random.nextInt(upperBound); |
| 50 | + result.add(randomInt); |
| 51 | + print(String.valueOf(randomInt) + " "); |
| 52 | + } |
| 53 | + println(); |
| 54 | + return result; |
| 55 | + } |
| 56 | + |
| 57 | + //this one has two other overloaded methods as above |
| 58 | + public static List<Integer> uniqueIntArrayGenerator(int size) { |
| 59 | + List<Integer> result = new ArrayList<Integer>(); |
| 60 | + for (int i = 0; i < size; i++) { |
| 61 | + result.add(i); |
| 62 | + } |
| 63 | + println(); |
| 64 | + return result; |
| 65 | + } |
| 66 | + |
| 67 | +// @Notes(context = "I'm assuing only classes in this PACKAGE will call the following two methods, so just leave the modifier as default, i.e. no public, private, or protected.") |
| 68 | + public |
| 69 | + static void printWhitespaces(int count) { |
| 70 | + for (int i = 0; i < count; i++) |
| 71 | + System.out.print(" "); |
| 72 | + } |
| 73 | + |
| 74 | + public static <T> boolean isAllElementsNull(List<T> list) { |
| 75 | + for (Object object : list) { |
| 76 | + if (object != null) |
| 77 | + return false; |
| 78 | + } |
| 79 | + |
| 80 | + return true; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * If you want to print the reversed list out, you need to return the |
| 85 | + * reversed list's head, which was the end node of the original node. using |
| 86 | + * the following function. |
| 87 | + */ |
| 88 | + public static ListNode reverseList(ListNode head) { |
| 89 | + if (head == null || head.next == null) { |
| 90 | + return head; |
| 91 | + } |
| 92 | + ListNode previous, current, next; |
| 93 | + previous = head; |
| 94 | + current = head.next; |
| 95 | + while (current != null) { |
| 96 | + next = current.next; |
| 97 | + current.next = previous; |
| 98 | + previous = current; |
| 99 | + current = next; |
| 100 | + } |
| 101 | + head.next = null; |
| 102 | + return previous; |
| 103 | + } |
| 104 | + |
| 105 | + public static void printList(ListNode head) { |
| 106 | + System.out.println("--------------------------------------------"); |
| 107 | + while (head != null) { |
| 108 | + System.out.print(head.val); |
| 109 | + head = head.next; |
| 110 | + } |
| 111 | + System.out.println(); |
| 112 | + } |
| 113 | + |
| 114 | +} |
0 commit comments