Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@JoyceeLee
Created July 2, 2014 16:59
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 JoyceeLee/9bc2f720dac8e06c69a1 to your computer and use it in GitHub Desktop.
Save JoyceeLee/9bc2f720dac8e06c69a1 to your computer and use it in GitHub Desktop.
/*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