CtCi 2.6: Implement a function to check if a linked list is a palindrome I would appreciate feedback on the linked list implementation and usage below for question 2.6 from Cracking the coding interview book.

struct Node {
    char value;
    shared_ptr&lt;Node&gt; next;
    Node(char v) : value(v) {};
};

shared_ptr&lt;Node&gt; head;

void createSampleList(string str)
{
    auto ptr = head;
    for (const char ch : str)
    {
        if(!head) {
            head = make_shared&lt;Node&gt;(ch);
            ptr = head;
        }
        else {
            ptr-&gt;next = make_shared&lt;Node&gt;(ch);
            ptr = ptr-&gt;next;
        }
    }
}

bool isPalindrome() {

    auto slow = head;
    auto fast = head;
    stack&lt;char&gt; tempStack;

    while (fast != nullptr &amp;&amp; fast-&gt;next != nullptr)
    {
        tempStack.push(slow-&gt;value);
        slow = slow-&gt;next;
        fast = fast-&gt;next-&gt;next;
    }

    if(fast != nullptr)
        slow = slow-&gt;next;

    while (slow != nullptr)
    {
       char reverseVal = tempStack.top();
        tempStack.pop();
        if(reverseVal != slow-&gt;value)
            return false;
        slow = slow-&gt;next;
    }

    return true;
}
Don't use using namespace std;. See any other C++ question for the reasons.

The declaration createSampleList(string str) means that when you pass a string to that function, the whole string will be copied. That's unnecessary. Declaring the function as createSampleList(const string &amp;str) is much more efficient.

The variable name tempStack can be improved. The word temp should not appear here at all, since it doesn't explain anything about the code.

I was first confused on why you called tempStack.pop() two times in a row. But then I saw that it was top and pop. There's nothing you can do against that, since the C++ API just doesn't provide the method that combines these two. Maybe you could move the tempStack.pop() below the if sentence.

The char reverseVal line is not indented properly. Let your IDE fix the indentation.

After each if, there should be a space. Only for function calls the space should be omitted.

A general rule is that you should not use global variables. The global variable in this case is head. It is better to pass such variables as parameters. This corresponds to natural language speaking, where you would ask Is the word asdfdsa a palindrome? instead of Is the word written at the wall over there a palindrome?. This makes your code much more flexible.

The rest of the code looks great for learning algorithms. If you want to use that code in practice, it should be much shorter. Something like this:

#include &lt;cassert&gt;
#include &lt;string&gt;

bool is_palindrome(const std::string &amp;str)
{
    std::string rev(str.rbegin(), str.rend());
    return str == rev;
}

int main()
{
    assert(is_palindrome(""));
    assert(is_palindrome("a"));
    assert(is_palindrome("aa"));
    assert(!is_palindrome("ab"));
    assert(!is_palindrome("aaabcaaa"));
}