1. // 用栈的方法,把链表放入栈中,再弹出来同时打印对应链表值对比看是否相等
    2. public static Boolean IsHuiWen(Node head){
    3. if(head == null || head.next == null){
    4. return true;
    5. }
    6. Node temp =head;
    7. Stack<Node> stack = new Stack<>();
    8. // 链表 入栈
    9. while(temp != null){
    10. stack.push(temp);
    11. temp = temp.next;
    12. }
    13. while(!stack.isEmpty()){
    14. Node pop = stack.pop();
    15. if(pop.value != head.value){
    16. return false;
    17. }
    18. head = head.next;
    19. }
    20. return true;
    21. }
    22. // 用快慢指针找出中点,一半部分入栈
    23. public static Boolean isHuiWen2(Node head){
    24. if(head == null || head.next == null){
    25. return true;
    26. }
    27. // 快慢指针,快指针结束后,慢指针就在中点
    28. Node slow = head;
    29. Node fast = head;
    30. while(slow.next != null && fast.next.next != null){
    31. slow = slow.next;
    32. fast = fast.next.next;
    33. }
    34. Stack<Node> stack = new Stack<>();
    35. while(slow != null){
    36. stack.push(slow);
    37. slow = slow.next;
    38. }
    39. while(!stack.isEmpty()){
    40. if(stack.pop().value != head.value){
    41. return false;
    42. }
    43. head = head.next;
    44. }
    45. return true;
    46. }