-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinked List 1:Palindrome LinkedList
47 lines (35 loc) · 1.09 KB
/
Linked List 1:Palindrome LinkedList
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
public class Solution {
public static boolean isPalindrome_2(LinkedListNode<Integer> head) {
LinkedListNode<Integer> temp=head,mid,h2;
mid=midPoint(temp);
h2=mid.next ;
mid.next=null;
h2=reverseIt(h2);
boolean flag=false;
while(temp!=null && h2!=null)
{ flag=false;
if(temp.data.equals(h2.data))
flag=true;
temp=temp.next;
h2=h2.next;
}
return flag;
}
private static LinkedListNode<Integer> reverseIt(LinkedListNode<Integer> head)
{ if(head==null || head.next==null)
return head;
LinkedListNode<Integer> tail=head.next;
LinkedListNode<Integer> ans=reverseIt(head.next);
tail.next=head;
head.next=null;
return ans;
}
private static LinkedListNode<Integer> midPoint(LinkedListNode<Integer> head){
LinkedListNode<Integer> slow=head,fast=head;
while(fast.next!=null && fast.next.next!=null)
{
fast=fast.next.next;
slow=slow.next;
}
return slow;}
}