1

LeetCode 234. 回文链表

大家好,我是灵魂画师--茄子。技术水平一般,喜欢画画。

开始今天的正题。

输入: 1->2
输出: false
输入: 1->2->2->1
输出: true

leetCode234. 回文链表

解题思路:
1.双指针迭代

题解:

/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var isPalindrome = function(head) {
    let quick = head;
    let slow = head;
    let pre = null;
    while(quick && quick.next){
        quick = quick.next.next;
        let next = slow.next;
        slow.next = pre;
        pre = slow;
        slow = next;
    }
    if(quick){ // 这里需要处理是奇数的情况
        slow = slow.next;
    }
    while(slow && pre){
        if(pre.val !== slow.val){
            return false
        }
        pre = pre.next;
        slow = slow.next;
    }
    return true
};

以上就是我的思路以及解法了,希望大家喜欢,我会继续努力的ヾ(◍°∇°◍)ノ゙。


灵魂画师_茄子
11 声望4 粉丝