Determine if a Linked List is a Palindrome

Given a singly linked list, this code problem determines if the linked list is a palindrome.

Problem

Given a singly linked list, determine if it is a palindrome.
Example input: 
1->2->2->1
Example output: 
true

Solution

This solution is in C#. Our A.I. can create solutions in multiple languages.
by kayakane
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //create a linked list
            LinkedList<int> list = new LinkedList<int>();
            list.AddFirst(1);
            list.AddLast(2);
            list.AddLast(2);
            list.AddLast(1);

            //create a stack
            Stack<int> stack = new Stack<int>();

            //push the first half of the linked list into the stack
            int count = 0;
            foreach (int i in list)
            {
                if (count < list.Count / 2)
                {
                    stack.Push(i);
                }
                count++;
            }

            //pop the stack and compare the popped value to the second half of the linked list
            //if the values are not equal, then the linked list is not a palindrome //if the values are equal, then the linked list is a palindrome
            count = 0;
            foreach (int i in list)
            {
                if (count >= list.Count / 2)
                {
                    if (stack.Pop() != i)
                    {
                        Console.WriteLine("false");
                        break;
                    }
                }
                count++;
            }
            Console.WriteLine("true");
        }
    }
}

A.I. Evaluation of the Solution

The candidate's solution correctly determines whether a singly linked list is a palindrome. The candidate uses a stack to store the first half of the linked list, and then compares the values in the stack to the second half of the linked list. If the values are not equal, then the linked list is not a palindrome. This is a correct approach to solving the problem.

Evaluated at: 2022-11-21 14:16:11