Created
June 30, 2014 16:29
-
-
Save bitcpf/3ed5522deab119c1f576 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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