Implement a function to check if a linked list is a palindrome in c++ This is working example, I'm interested in mostly in knowing if there is any c++ related improvements you guys suggest? Also, do you think its better to solve this question iteratively or interviewer would prefer some recursive solution (assuming if that is possible)? Any other suggestions from interview point of view welcome?

#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;vector&gt;
#include &lt;cstdlib&gt;

template&lt;typename valT&gt;
struct node
{
  valT val;
  node&lt;valT&gt; *next;
  node() { next = nullptr; }
  node(valT v) : val(v),next(nullptr) { }
  ~node() { next = nullptr; }
};

using myNode = node&lt;int&gt;;
using myNodePtr = myNode *;
myNodePtr head = nullptr; 

void showLL(myNodePtr head)
{
 std::cout &lt;&lt; "Linked List =&gt;";
 while(head){
   std::cout &lt;&lt; head-&gt;val &lt;&lt; ", ";
   head = head-&gt;next;
 }
 std::cout &lt;&lt; std::endl;
}

int main(int argc, char **argv)
{
  myNodePtr prev = nullptr;
  for( int i = 1; i &lt; argc ; i++ ) {
    myNodePtr tmp = new myNode(strtol(argv[i], NULL, 10));
    if(!head) {
      head = tmp;
    }
    if(prev){
      prev-&gt;next = tmp;
    } 
    prev = tmp;
  }

  std::cout &lt;&lt; "Input "  &lt;&lt; std::endl;
  showLL(head);

  //Find middle
  //Push on stack until you find middle
  //Stop fast runner and pop back and compare and break if not same

  //Palindrome loop
  myNodePtr middleNode = nullptr;
  std::vector&lt;int&gt; stack;
  for(myNodePtr slowRunner = head, fastRunner = head; slowRunner; slowRunner = slowRunner-&gt;next) {
    if(!middleNode) {
      stack.push_back(slowRunner-&gt;val);

      if(fastRunner-&gt;next) {
        if(fastRunner-&gt;next-&gt;next) { //odd
          fastRunner = fastRunner-&gt;next-&gt;next;
          if(!fastRunner-&gt;next) {
            slowRunner = slowRunner-&gt;next;
          }
        } else { //even
          fastRunner = fastRunner-&gt;next;
        }
        if(!fastRunner-&gt;next) {
          middleNode = slowRunner;
          std::cout &lt;&lt; "middle node =" &lt;&lt; slowRunner-&gt;val &lt;&lt; std::endl;
        }
      }
    } else {
      //even odd
      if( stack.back() != slowRunner-&gt;val) {
        std::cout &lt;&lt; "Not a palindrom!" &lt;&lt; std::endl;
        return 1; //delete all linked lists
      }
      stack.pop_back();
    }
  }

  std::cout &lt;&lt; "is Palindrome"&lt;&lt; std::endl;
}
Building linked list from command line arguments

The multiple conditionals inside this loop is kind of ugly,
because head and prev will only ever be null once:


  for( int i = 1; i &lt; argc ; i++ ) {
    myNodePtr tmp = new myNode(strtol(argv[i], NULL, 10));
    if(!head) {
      head = tmp;
    }
    if(prev){
      prev-&gt;next = tmp;
    } 
    prev = tmp;
  }



You could avoid the unnecessary conditional evaluations by lifting the special treatment of the head outside the loop:

  myNodePtr head = nullptr;
  if (argc &gt; 1) {
    head = new myNode(strtol(argv[1], NULL, 10));
  }
  myNodePtr node = head;
  for (int i = 2; i &lt; argc; i++) {
    node-&gt;next = new myNode(strtol(argv[i], NULL, 10));
    node = node-&gt;next;
  }


Iterating until the middle and verifying the rest

Iterating until the middle could be written simpler:

  std::vector&lt;int&gt; stack;
  myNodePtr slow = head;
  myNodePtr fast = head;
  while (fast &amp;&amp; fast-&gt;next) {
    stack.push_back(slow-&gt;val);
    slow = slow-&gt;next;
    fast = fast-&gt;next-&gt;next;
  }


At this point,
if the length is odd, slow will be pointing to the single middle element,
that should be skipped.
The length is odd if fast is not null, so you can write:

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


Finally, to check that the rest of the list matches the content of the stack:

  while (!stack.empty()) {
    if (stack.back() != slow-&gt;val) {
      std::cout &lt;&lt; "Not a palindrom!" &lt;&lt; std::endl;
      return 1;
    }
    stack.pop_back();
    slow = slow-&gt;next;
  }


This alternative implementation uses much fewer conditions and cleanly separates the interesting steps, so I think it's easier to read.

From an interview point of view

At an interview,
if you write an iterative solution,
the interviewer is likely to ask to also write a recursive solution.
And vice versa.
You need to be able to ready to do both,
and discuss about the trade-offs of each technique.

This solution would get important criticism at an interview for several reasons:


Large chunk of code inside main method, instead of separated to small methods with single simple clear purpose
Knowledge of memory management is not demonstrated (the program simply relies on cleanup on exit)