Created
January 29, 2016 16:41
-
-
Save berkayk/81fdad62a2306f247d4d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class Node { | |
Node next; | |
Node prev; | |
int data; | |
public Node(int value) { | |
this.data = value; | |
} | |
} | |
public boolean isPalindrome(Node head) { | |
if (head == null) | |
return false; | |
Node tail; | |
Node node = head; | |
while (node != null) { | |
Node prev = node; | |
node = node.next; | |
if (node != null) { | |
node.prev = prev; | |
} | |
else { | |
tail = prev; | |
} | |
} | |
while (head != tail && tail.next != head) { | |
if (head.data == tail.data) { | |
head = head.next; | |
tail = tail.prev; | |
} | |
else | |
return false; | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment