Skip to content

added class for Palindrome and re-edited SinglyLinkedList #75

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Misc/Palindrome.java
Original file line number Diff line number Diff line change
@@ -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)));
}

}
30 changes: 15 additions & 15 deletions data_structures/Lists/SinglyLinkedList.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
class SinglyLinkedList{
/**Head refered to the front of the list */
private LinkForLinkedList head;
private Node head;

/**
* Constructor of SinglyLinkedList
Expand All @@ -29,18 +29,18 @@ 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
}

/**
* This method deletes an element at the head
*
* @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;
}
Expand All @@ -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();
Expand Down Expand Up @@ -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;
}

}
}