Implement a function to check if a linked list is a palindrome in c++
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)