Skip to content

Instantly share code, notes, and snippets.

@bitcpf
Created June 30, 2014 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bitcpf/3ed5522deab119c1f576 to your computer and use it in GitHub Desktop.
Save bitcpf/3ed5522deab119c1f576 to your computer and use it in GitHub Desktop.
package cc150_2_7;
// Created by bitcpf
public class PalindromeCheck {
public static boolean Palindrome(Node head){
if(head == null){
return false;
}
Node hd_back = head;
// Create new list, reverse the list
Node temp = new Node(head.item);
Node nw_head = null;
while(head.next != null){
temp.next = nw_head;
nw_head = temp;
head = head.next;
temp = new Node(head.item);
}
temp = new Node(head.item);
temp.next = nw_head;
nw_head = temp;
// Compare the two lists
head = hd_back;
int flag = 0;
while(nw_head.next != null && head.next != null){
if(nw_head.item != head.item){
return false;
}
nw_head = nw_head.next;
head = head.next;
flag ++;
}
return true;
}
public static void printlist(Node head){
if(head.item != null){
while(head.next != null){
System.out.print(head.item);
head = head.next;
}
System.out.println(head.item);
}
}
public static void main(String[] args){
String test = "abcdefgfedcba";
char[] test_char = test.toCharArray();
Node head = new Node(test_char[0]);
Node temp = head;
for(int i= 1; i < test.length(); i++){
temp.next = new Node(test_char[i]);
temp = temp.next;
}
printlist(head);
System.out.println(Palindrome(head));
}
}
public class Node<Item> {
Item item = null;
Node next = null;
public Node(Item item){
this.item = item;
this.next = null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment