检查单链表是否是回文

时间:2017-05-17 00:18:45

标签: c# linked-list singly-linked-list palindrome

这是我的代码。

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!");
            }
        }
    }
}

我的代码每次都为回文函数返回false,除非我在链表中​​只输入一个值。

另请告诉我,List<int>的方法是否合适,因为我需要进行回文检查。

1 个答案:

答案 0 :(得分:0)

谢谢您的评论,这就是我解决的问题。

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;
    }