题解 | #判断一个链表是否为回文结构#
判断一个链表是否为回文结构
http://www.nowcoder.com/practice/3fed228444e740c8be66232ce8b87c2f
import java.util.*;
/*
- public class ListNode {
- int val;
- ListNode next = null;
- } */
public class Solution {
//利用栈FILO的特性,完成回文判断
public boolean isPail (ListNode head) {
// write code here
Stack<Integer> tempStack = new Stack<>();
ListNode tmpNode = head;
while(tmpNode != null){
tempStack.push(tmpNode.val);
tmpNode = tmpNode.next;
}
while(head != null){
if(head.val != tempStack.pop()){
return false;
}
head = head.next;
}
return true;
}
}