Given a linked list with string data, check whether the combined string formed is palindrome. If the combined string is palindrome then return true otherwise return false.
Example:
Input:
Output : true
Explanation: As string "abcddcba" is palindrome the function should return true.
Input:
Output : false
Explanation: As string "abcdba" is not palindrome the function should return false.
Expected Time Complexity: O(n)
Expected Auxiliary Space: O(n)
Constraints:
1 <= Node.data.length<= 103
1<=list.length<=103
Approach 01:
-
C++
-
Python
class Solution {
public:
bool compute(Node* head) {
string result = "";
while (head != nullptr) {
result += head->data;
head = head->next;
}
// Check if the result string is equal to its reverse
return result == string(result.rbegin(), result.rend());
}
};
def compute(head):
result = ''
while(head != None):
result += head.data
head = head.next
return result == result[::-1]

