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     }

 

posted @ 2017-10-24 16:28  daniel456  阅读(111)  评论(0编辑  收藏  举报