234. Palindrome Linked List

Posted andywu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了234. Palindrome Linked List相关的知识,希望对你有一定的参考价值。

Given a singly linked list, determine if it is a palindrome.

题目含义:给定一个单列表,判断是否构成回文

 

 1     public boolean isPalindrome(ListNode head) {
 2         Stack<Integer> valueStack = new Stack<>();
 3         if (head == null) return true;
 4         ListNode runer = head;
 5         ListNode walker = head;
 6         while (runer.next != null && runer.next.next != null)
 7         {
 8             valueStack.push(walker.val);
 9             runer = runer.next.next;
10             walker = walker.next;
11         }
12         if (runer.next != null)
13         {
14 //            列表有偶数个节点
15             if (walker.val != walker.next.val) return false;
16             walker = walker.next.next;
17         }else{
18             walker = walker.next;
19         }
20         while (walker != null)
21         {
22             if (walker.val != valueStack.pop()) return false;
23             walker = walker.next;
24         }
25         return true;
26         
27     }

 

以上是关于234. Palindrome Linked List的主要内容,如果未能解决你的问题,请参考以下文章

234. Palindrome Linked List

LC.234.Palindrome Linked List

234. Palindrome Linked List

Leetcode 234. Palindrome Linked List

234. Palindrome Linked List

234. Palindrome Linked List