Search code examples
c#linked-listsingly-linked-listpalindrome

Checking if singly-linked list is palindrome or not


This is my code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LinkedList
{
    public class Node
    {
        public int data;
        public Node next;
        public Node(int data)
        {
            this.data = data;
            next = null;
        }
    }

    public class MyList
    {
        public Node head;

        public MyList()
        {
            head = null;
        }

        public void addNode(int data)
        {
            if(head == null)
            {
                head = new Node(data);

            }
            else
            {
                Node temp = new Node(data);              

                Node current = head;
                while(current.next != null)
                {
                    current = current.next;
                }
                current.next = temp;
            }
        }

        public void print()
        {
            if(head == null)
            {
                Console.WriteLine("List is already empty!");
            }
            else
            {
                Node current = head;
                while (current != null)
                {
                    Console.Write("|" + current.data + "|-> ");
                    current = current.next;
                }
                Console.WriteLine();
            }
        }

        public void addToStart(int data)
        {
            if(head == null)
            {
                head = new Node(data);
            }
            else
            {
                Node temp = new Node(data);
                temp.next = head;
                head = temp;
            }
        }

        public void addSorted(int data)
        {
            if(head == null)
            {
                head = new Node(data);
            }
            else if(data < head.data)
            {
                addToStart(data);
            }
            else
            {
                Node current = head.next;
                Node previous = head;
                Node temp = new Node(data);

                while(current != null)
                {
                    if(data < current.data)
                    {
                        previous.next = temp;
                        temp.next = current;
                        break;
                    }
                    previous = current;
                    current = current.next;
                }
            }
        }

        public void removeLast()
        {
            if(head == null)
            {
                Console.WriteLine("List is already empty!");
            }
            else if(head.next == null)
            {
                head = null;
            }
            else
            {
                Node current = head.next;
                Node previous = head;

                while(current.next != null)
                {
                    previous = current;
                    current = current.next;
                }
                previous.next = null;
            }
        }

        public bool isPalindrome()
        {
            List<int> arr1 = new List<int>();
            int i = 0;
            Node current = head;

            while (current != null)
            {
                arr1.Add(current.data);
                current = current.next;
                i++;
            }

            int[] arr3 = arr1.ToArray();
            int count = i;
            int[] arr2 = new int[count];
            int j = 0;

            for (int x = i - 1; x >= 0; x--)
            {
                arr2[j] = arr3[x];
            }

            for (int k = 0; k < count; k++)
            {
                if (arr3[k] != arr2[k])
                {
                    return false;
                }
            }
            return true;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            MyList a = new MyList();
            a.addNode(1);
            a.addNode(2);
            a.addNode(5);
            a.addNode(2);
            a.addNode(1);
            a.print();

            if(a.isPalindrome())
            {
                Console.WriteLine("Linked List is Palindrome!");
            }
            else
            {
                Console.WriteLine("Linked List is Not Palindrome!");
            }
        }
    }
}

My code returns false for the palindrome function every time except when I enter only one value in the linked list.

Also let me know if my method of List<int> is okay or not because I needed it for the palindrome check.


Solution

  • Thankyou for your comments, This is how i solved it.

    public bool isPalindrome()
        {
            int i = 0;
            Node current = head;
            Node temp = head;
    
            while (temp != null)
            {
                temp = temp.next;
                i++;
            }
    
            int[] arr1 = new int[i];
            int count = i;
    
            for (int j = 0; j < count; j++)
            {
                arr1[j] = current.data;
                current = current.next;
            }
    
            int[] arr2 = new int[count];
            int z = 0;
    
            for (int x = i - 1; x >= 0; x--)
            {
                arr2[z] = arr1[x];
                z++;
            }
    
            for (int x = 0; x < count; x++)
            {
                if (arr1[x] != arr2[x])
                {
                    return false;
                }
            }
            return true;
        }