/**
* 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 = ; 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
补充一个python的实现:
class Solution:
def isPalindrome(self, head: ListNode) -> bool:
lists = []
while head != None:
lists.append(head.val)
head = head.next
i,j = ,len(lists)-
while i < j:
if lists[i] != lists[j]:
return False
i +=
j -=
return True