Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static LLNode reverseList(LLNode head){
- if (head == null){
- return null;
- }
- if (head.next == null){
- return head;
- }
- LLNode headNext = head.next;
- head.next = null;
- LLNode reverseHeadNext = reverseList(headNext);
- headNext.next = head;
- return reverseHeadNext;
- }
- public static boolean isPalindrome(LLNode head) {
- LLNode headCopy=head;
- LLNode reverseHead=reverseList(headCopy);
- LLNode tmp1=head;
- LLNode tmp2=reverseHead;
- while (tmp1.next!=null){
- if (tmp1.data!=tmp2.data){
- return false;
- }
- else{
- tmp1=tmp1.next;
- tmp2=tmp2.next;
- }
- }
- if (tmp1!=tmp2){
- return false;
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment