diff --git a/build/classes/TechInterview/PalindromeFinder.class b/build/classes/TechInterview/PalindromeFinder.class new file mode 100644 index 0000000..c8d65fe Binary files /dev/null and b/build/classes/TechInterview/PalindromeFinder.class differ diff --git a/build/classes/TechInterview/SinglyLinkedList$Node.class b/build/classes/TechInterview/SinglyLinkedList$Node.class index 6975b8f..398adc1 100644 Binary files a/build/classes/TechInterview/SinglyLinkedList$Node.class and b/build/classes/TechInterview/SinglyLinkedList$Node.class differ diff --git a/build/classes/TechInterview/SinglyLinkedList.class b/build/classes/TechInterview/SinglyLinkedList.class index ecb0c0d..59c2311 100644 Binary files a/build/classes/TechInterview/SinglyLinkedList.class and b/build/classes/TechInterview/SinglyLinkedList.class differ diff --git a/build/classes/TechInterview/StackLinkedList$Node.class b/build/classes/TechInterview/StackLinkedList$Node.class index 26beeb5..825c936 100644 Binary files a/build/classes/TechInterview/StackLinkedList$Node.class and b/build/classes/TechInterview/StackLinkedList$Node.class differ diff --git a/build/classes/TechInterview/StackLinkedList.class b/build/classes/TechInterview/StackLinkedList.class index d42b32c..823fda5 100644 Binary files a/build/classes/TechInterview/StackLinkedList.class and b/build/classes/TechInterview/StackLinkedList.class differ diff --git a/src/TechInterview/PalindromeFinder.java b/src/TechInterview/PalindromeFinder.java new file mode 100644 index 0000000..5622a4e --- /dev/null +++ b/src/TechInterview/PalindromeFinder.java @@ -0,0 +1,119 @@ +package TechInterview; + +/** + * Given a linkedlist, find if the linkedlist is a palindrome. + * + * Input: Generic linkedlist of any size + * + * Output: If the linkedlist is a palindrome, isPalindrome returns true. + * Otherwise, isPalindrome returns false. + * + */ +public class PalindromeFinder { + + private SinglyLinkedList list; + private boolean isEven; + private StackLinkedList stack; + + public PalindromeFinder(SinglyLinkedList list) { + this.list = list; + stack = new StackLinkedList(); + + //Checks if the linkedlist has an even number of nodes + if(list.size() % 2 != 0) + isEven = false; + else + isEven = true; + } + + /* + * Method to change the linkedlist that needs to be checked. + * Allows one PalindromeFinder to check multiple lists for palindromes + */ + public void changeLinkedList(SinglyLinkedList list) { + this.list = list; + stack = new StackLinkedList(); + + if((list.size() % 2) != 0) + isEven = false; + else + isEven = true; + } + + /* + * Uses a stack to store the first half of the linkedlist. + * Then, compares the stored elements with the second half of the linkedlist. + * @return boolean true if the linkedlist is a palindrome, false otherwise. + */ + public boolean isPalindrome() { + int i = 0; + //Stores the first half of the linkedlist into the stack + while(i < (list.size() / 2)) { + stack.push(list.get(i)); + i++; + } + //Deals with cases where there is an odd number of elements in the original linkedlist + if(!isEven) + i++; + + //Attempts to match each element remaining in the list to each element stored in the stack + while(i < list.size()) { + //If the element in the list does not match the element in the stack, then exit the loop + if(!(list.get(i).equals(stack.pop()))) + break; + //If the loop has reached the end of the list without any mismatches, then the list is a palindrome + if(i == list.size() - 1) + return true; + + i++; + } + //If at any point the previous while loop breaks, then the list is not a palindrome, and return false. + return false; + } + + + + public static void main(String[] args) { + /* + * Tests: + * evenPalin (first print statement) should be true (tests lists of even sizes) + * oddPalin (second print statement) should be true (tests lists of odd sizes) + * notPalin (third print statement) should be false (tests lists that aren't palindromes) + * empty (fourth print statement) should be false (tests lists that are empty) + */ + SinglyLinkedList evenPalin = new SinglyLinkedList<>(); + SinglyLinkedList oddPalin = new SinglyLinkedList<>(); + SinglyLinkedList notPalin = new SinglyLinkedList<>(); + SinglyLinkedList empty= new SinglyLinkedList<>(); + + //Adds the first set of elements to the linkedlist + for(int i = 1; i < 6; i++) { + evenPalin.add(i); + oddPalin.add(i); + notPalin.add(i); + } + + //Adds an extra element for the odd list + oddPalin.add(0); + + //Adds the remaining half of the elements to the linked list + //notPalin has elements shifted up by one in order to have it not be a palindrome + for(int i = 5; i > 0; i--) { + evenPalin.add(i); + oddPalin.add(i); + notPalin.add(i+1); + } + + PalindromeFinder palinFinder = new PalindromeFinder<>(evenPalin); + System.out.println(palinFinder.isPalindrome()); + + palinFinder.changeLinkedList(oddPalin); + System.out.println(palinFinder.isPalindrome()); + + palinFinder.changeLinkedList(notPalin); + System.out.println(palinFinder.isPalindrome()); + + palinFinder.changeLinkedList(empty); + System.out.println(palinFinder.isPalindrome()); + } +} \ No newline at end of file diff --git a/src/TechInterview/SinglyLinkedList.java b/src/TechInterview/SinglyLinkedList.java index 1e35c9f..7e3c9e0 100644 --- a/src/TechInterview/SinglyLinkedList.java +++ b/src/TechInterview/SinglyLinkedList.java @@ -44,7 +44,7 @@ public Node remove(int index) { } public E get(int index) { - if (index > 0 && index < size) { + if (index >= 0 && index < size) { Node curr = head; for (int i = 0; i <= index; i++) { curr = curr.next; @@ -81,6 +81,10 @@ public String toString() { return sb.toString(); } + public int size() { + return size; + } + public class Node { private Node next;