From 6d3c8464c453b5d5fff1536c2709d77b3d4b60c9 Mon Sep 17 00:00:00 2001 From: earngpi Date: Sun, 16 Jul 2017 22:14:29 +0700 Subject: [PATCH 1/2] create Palindrome.java --- Misc/Palindrome.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Misc/Palindrome.java 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))); + } + + } From d6b9e548eff15c3d0e56892f7b2fbf2c25c5c69d Mon Sep 17 00:00:00 2001 From: earngpi Date: Sun, 16 Jul 2017 22:19:30 +0700 Subject: [PATCH 2/2] update singly linked list --- data_structures/Lists/SinglyLinkedList.java | 30 ++++++++++----------- 1 file changed, 15 insertions(+), 15 deletions(-) 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 +}