1. /**
    2. * Definition for singly-linked list.
    3. * function ListNode(val) {
    4. * this.val = val;
    5. * this.next = null;
    6. * }
    7. */
    8. /**
    9. * @param {ListNode} head
    10. * @return {boolean}
    11. */
    12. var isPalindrome = function (head) {
    13. if(head == null || head.next == null){
    14. return true;
    15. }
    16. let left = head;
    17. let midNode = findMid(head);
    18. let right = reverse(midNode);
    19. // 开始比较
    20. while(right != null){
    21. if(left.val !== right.val){
    22. return false;
    23. }
    24. left = left.next;
    25. right = right.next;
    26. }
    27. return true;
    28. };
    29. // 双指针找到链表中间结点
    30. var findMid = function (head){
    31. let fast, slow;
    32. fast = slow = head;
    33. while (fast != null && fast.next != null && slow != null) {
    34. slow = slow.next;
    35. fast = fast.next.next;
    36. }
    37. return slow;
    38. }
    39. // 翻转以head为头的链表
    40. var reverse = function (head) {
    41. let pre = null, cur = head;
    42. while(cur!=null){
    43. let next = cur.next;
    44. cur.next = pre;
    45. pre = cur;
    46. cur = next;
    47. }
    48. return pre;
    49. }