Created
July 2, 2014 16:59
-
-
Save JoyceeLee/9bc2f720dac8e06c69a1 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
/*2.7 Implement a function to check if a linked list is a palindrome.*/ | |
public class Solution { | |
public boolean isPalindrome(ListNode head) { | |
ListNode prehead = new ListNode(-1); | |
prehead.next = null; | |
ListNode l1 = head; | |
while(l1!=null) { | |
ListNode tmp = new ListNode(l1.val); | |
tmp.next = prehead.next; | |
prehead.next = tmp; | |
l1 = l1.next; | |
} | |
prehead = prehead.next; | |
while(head!=null && prehead!=null) { | |
if(head.val!=prehead.vl) { | |
return false; | |
} | |
head = head.next; | |
prehead = prehead.next; | |
} | |
if(head!=null || prehead!=null) | |
return false; | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment