linked list of strings forms a palindrome

PROBLEM :

Given a linked list of strings having n nodes check to see whether the combined string formed is palindrome or not.

Example :

Input  : a -> bc -> d -> dcb -> a -> NULL
Output : True
String "abcddcba" is palindrome.

Output : a -> bc -> d -> ba -> NULL
Output : False
String "abcdba" is not palindrome.


Input:
You have to complete the method which takes one argument: the head of the linked list . You should not read any input from stdin/console.. There are multiple test cases. For each test case, this method will be called individually.

Output:
Your function should return True if the combined string is pallindrome else it should return False.

Constraints:
1 <=T<= 231
1 <=n<= 1000

Example:
Input :
2
5
a bc d dcb a
4
a bc d ba


Output :
True
False

Explanation:
case 1 : as String "abcddcba" is palindrome the function should return true
case 2 : as String "abcdba" is not palindrome the function should return false

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

/*

The structure of linked list is the following

struct Node
{
String data;
Node* next;
};

*/


bool strPal(string) ;

bool Compute(struct Node* root)
{
    string str ;
   
    if(root==NULL)
        return true ;
       
    while(root!=NULL)
    {
        str=str+root->data ;
        root=root->next ;
    }
   
    return strPal(str) ;
}

bool strPal(string str)
{
    int l,i,j ;
    l= str.length() ;
   
    i=0 ;
    j=l-1 ;
   
    while(i<j)
    {
        if(str[i]!=str[j])
            return false ;
        else
        {
            i++ ;
            j-- ;
        }
    }
    return true ;
}


---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )