回文链表
    请判断一个链表是否为回文链表。
    示例 1:
    输入: 1->2
    输出: false
    示例 2:
    输入: 1->2->2->1
    输出: true
    进阶:
    你能否用 O(n) 时间复杂度和 O(1) 空间复杂度解决此题?
    解析:见org 第三种解法
    作者:力扣 (LeetCode)
    链接:https://leetcode-cn.com/leetbook/read/top-interview-questions-easy/xnv1oc/
    来源:力扣(LeetCode)
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

    1. /**
    2. * Definition for singly-linked list.
    3. * public class ListNode {
    4. * int val;
    5. * ListNode next;
    6. * ListNode(int x) { val = x; }
    7. * }
    8. */
    9. class Solution {
    10. // org 时间空间复杂度都是 O(n)
    11. // equals [-129,-129]
    12. public boolean isPalindrome(ListNode head) {
    13. ArrayList<Integer> res = new ArrayList<Integer>();
    14. ListNode cur = head;
    15. while(cur != null){
    16. res.add(cur.val);
    17. cur = cur.next;
    18. }
    19. int front=0;
    20. int tail =res.size()-1;
    21. while(front<=tail){
    22. if(!res.get(front).equals(res.get(tail))){
    23. return false;
    24. }
    25. front++;
    26. tail--;
    27. }
    28. return true;
    29. }
    30. // //第一次 提交 针对奇数个元素 不成立。
    31. // public boolean isPalindrome(ListNode head) {
    32. // if(head==null){
    33. // return false;
    34. // }
    35. // Stack<Integer> res = new Stack<Integer>();
    36. // ListNode cur = head;
    37. // while(cur!=null){
    38. // if(!res.isEmpty()&&res.peek() == cur.val){
    39. // res.pop();
    40. // }
    41. // else{
    42. // res.add(cur.val);
    43. // }
    44. // cur = cur.next;
    45. // }
    46. // return res.isEmpty();
    47. // }
    48. }