diff --git a/Misc/Palindrome.java b/Misc/Palindrome.java new file mode 100644 index 000000000000..6017f0880a61 --- /dev/null +++ b/Misc/Palindrome.java @@ -0,0 +1,16 @@ +class Palindrome { + + public String reverseString(String x){ //*helper method + String output = ""; + for(int i=x.length()-1; i>=0; i--){ + output += x.charAt(i); //addition of chars create String + } + return output; + } + + + public Boolean isPalindrome(String x){ //*palindrome method, returns true if palindrome + return (x.equalsIgnoreCase(reverseString(x))); + } + + } diff --git a/data_structures/Lists/SinglyLinkedList.java b/data_structures/Lists/SinglyLinkedList.java index 4523739b3a98..c5a4ae08a444 100644 --- a/data_structures/Lists/SinglyLinkedList.java +++ b/data_structures/Lists/SinglyLinkedList.java @@ -14,7 +14,7 @@ */ class SinglyLinkedList{ /**Head refered to the front of the list */ - private LinkForLinkedList head; + private Node head; /** * Constructor of SinglyLinkedList @@ -29,9 +29,9 @@ public SinglyLinkedList(){ * @param x Element to be added */ public void insertHead(int x){ - LinkForLinkedList newLink = new LinkForLinkedList(x); //Create a new link with a value attached to it - newLink.next = head; //Set the new link to point to the current head - head = newLink; //Now set the new link to be the head + Node newNode = new Node(x); //Create a new link with a value attached to it + newNode.next = head; //Set the new link to point to the current head + head = newNode; //Now set the new link to be the head } /** @@ -39,8 +39,8 @@ public void insertHead(int x){ * * @return The element deleted */ - public LinkForLinkedList deleteHead(){ - LinkForLinkedList temp = head; + public Node deleteHead(){ + Node temp = head; head = head.next; //Make the second element in the list the new head, the Java garbage collector will later remove the old head return temp; } @@ -58,9 +58,9 @@ public boolean isEmpty(){ * Prints contents of the list */ public void display(){ - LinkForLinkedList current = head; + Node current = head; while(current!=null){ - current.displayLink(); + System.out.print(current.getValue()+" "); current = current.next; } System.out.println(); @@ -96,26 +96,26 @@ public static void main(String args[]){ * @author Unknown * */ -class LinkForLinkedList{ +class Node{ /** The value of the node */ public int value; /** Point to the next node */ - public LinkForLinkedList next; //This is what the link will point to + public Node next; //This is what the link will point to /** * Constructor * * @param valuein Value to be put in the node */ - public LinkForLinkedList(int valuein){ + public Node(int valuein){ value = valuein; } /** - * Prints out the value of the node + * Returns value of the node */ - public void displayLink(){ - System.out.print(value+" "); + public int getValue(){ + return value; } -} \ No newline at end of file +}