githubEdit

02(July) linked list of strings forms a palindrome

02. Linked List of Strings Forms a Palindrome

The problem can be found at the following link: Question Linkarrow-up-right

Problem Description

Given a linked list with string data, check whether the combined string formed is a palindrome. If the combined string is a palindrome, return true; otherwise, return false.

Example:

Input:

List: a -> bc -> d -> dcb -> a
Image

Output: ``` true ``` Explanation: The combined string "abcddcba" is a palindrome, so the function returns true.

My Approach

  1. Initialization:

    • Create a string ans to store the combined string formed from the linked list.

    • Initialize a pointer t to traverse the linked list.

  2. String Construction:

    • Traverse the linked list from head to end.

    • Append the data from each node to the string ans.

  3. Palindrome Check:

    • Use two pointers i and j to check the combined string for palindrome properties.

    • Compare the characters at positions i and j while moving i from the start and j from the end towards the middle.

    • If any pair of characters does not match, return false.

    • If all characters match, return true.

Time and Auxiliary Space Complexity

  • Expected Time Complexity: O(n), where n is the total length of the combined string formed from the linked list.

  • Expected Auxiliary Space Complexity: O(n), as we use a string to store the combined data from the linked list.

Code (C++)

Code (Java)

Code (Python)

Contribution and Support

For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questionsarrow-up-right. Let’s make this learning journey more collaborative!

⭐ If you find this helpful, please give this repository a star! ⭐


📍Visitor Count

Last updated