Linked list of strings forms a palindrome | Problem of the Day | GeeksForGeeks

Sdílet
Vložit
  • čas přidán 3. 08. 2024
  • 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)

Komentáře • 2

  • @mathematics3398
    @mathematics3398  Před měsícem

    def compute(head):
    string = ""
    while head:
    string += head.data
    head = head.next
    i = 0
    j = len(string) - 1
    while i < j:
    if string[i] != string[j]:
    return False
    i += 1
    j -= 1
    return True

  • @mathematics3398
    @mathematics3398  Před měsícem

    class Solution {
    public:
    bool compute(Node* head) {
    string str = "";
    while (head) {
    str += head->data;
    head = head->next;
    }
    for (int i = 0, j = str.length() - 1; i < j; i++, j--) {
    if (str[i] != str[j])
    return false;
    }
    return true;
    }
    };