首页 > 代码库 > leetcode234

leetcode234

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public int val;
 *     public ListNode next;
 *     public ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public bool IsPalindrome(ListNode head) {
        if (head == null)
            {
                return true;
            }
            else
            {
                Queue<int> Q = new Queue<int>();
                Stack<int> S = new Stack<int>();

                do
                {
                    Q.Enqueue(head.val);
                    S.Push(head.val);

                    head = head.next;
                }
                while (head != null);

                var len = S.Count;

                for (int i = 0; i < len; i++)
                {
                    var s = S.Pop();
                    var q = Q.Dequeue();
                    if (s != q)
                    {
                        return false;
                    }
                }
                return true;
            }
    }
}

https://leetcode.com/problems/palindrome-linked-list/#/description

leetcode234