package linkedlist.singly;
public class LinkListPalindromeCheck {
public static void main(String[] args) {
Node temp1 = new Node(1);
Node temp2 = new Node(2);
Node temp3 = new Node(3);
Node temp4 = new Node(2);
Node temp5 = new Node(1);
temp1.setNext(temp2);
temp2.setNext(temp3);
temp3.setNext(temp4);
temp4.setNext(temp5);
Node startNode = temp1;
LinkListPalindromeCheck linkListPalindromeCheck = new LinkListPalindromeCheck();
boolean isPalindrome = linkListPalindromeCheck.isPalindrome2(startNode,startNode);
System.out.println(startNode + " " + isPalindrome);
}
//Recursive way
public boolean isPalindrome2(Node left, Node right){
return isPalindromeRecursiveHelper(left, right)==null?false:true;
}
//Recursive way helper
public Node isPalindromeRecursiveHelper(Node left, Node right){
if(right==null){
return left;
}
left = isPalindromeRecursiveHelper(left, right.getNext());
if(left==null){
return null;
}
//To handle last recursive call before return.
//It says if left.getNext == null and if yes, then it means this is the last point control came here as in next iteration control is going to return,
//what happen if this check was not there, then it went ahead and did left = left.getNext(), which returns null and violate our assumption.
//So to check if this is last call and till last call if left!=null, it means we can safely return from here left as this will help us to return not null value.
if(left.getNext()==null && left!=null){
return left;
}
if(left.getData() == right.getData()){
return left.getNext();
}
return null;
}
//By reversing the list
public boolean isPalindrome2(Node startNode){
if(startNode==null)
return true;
Node head1=startNode;
Node head2=startNode;
Node previous=null;
//Finding Middle
while(head2!=null && head2.getNext()!=null){
previous = head1;
head1 = head1.getNext();
head2 = head2.getNext().getNext();
}
//Breaking the list into two and reversing the other half.
Node reverseHead=null;
if(head2!=null){
reverseHead = reverseList(head1.getNext());
}else{
reverseHead = reverseList(head1);
}
previous.setNext(null);
//compare two list now
boolean isPalindrome = true;
Node tempStart=startNode;
Node tempReverse=reverseHead;
while(tempReverse!=null){
if(tempStart.getData()!=tempReverse.getData()){
isPalindrome=false;
break;
}
tempStart=tempStart.getNext();
tempReverse=tempReverse.getNext();
}
//Again reverse the second half, so that it come back to original form
reverseHead = reverseList(reverseHead);
//Create LinkList back to original
if(head2!=null){
previous.setNext(head1);
head1.setNext(reverseHead);
}else{
previous.setNext(reverseHead);
}
return isPalindrome;
}
public Node reverseList(Node node){
Node a=null;
Node b=node;
while(b!=null){
Node c = node.getNext();
b.setNext(a);
a=b;
b=c;
}
return a;
}
}
Questions on Stack, Queues, Linkedlist, Binary Trees, Sorting, Searching, Graphs etc with solution using Java Language.
Sunday, 7 June 2015
To check if a singly linked list is palindrome.
Labels:
Linkedlist
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment