/** * 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
时间: 2024-12-14 04:31:33