请解释一下[slow.next, slow, head2] = [head2, slow.next, slow]

来源:5-10 【勤于思考,夯实学习成果】阶段思考题

迷失的小麦

2021-12-15

var isPalindrome = function (head) {
    if (head == null || head.next == null) {
        return true
    }
    let slow = head
    let fast = head
    let prev
    while (fast && fast.next) {
        prev = slow
        fast = fast.next.next
        slow = slow.next
    }
    prev.next = null // 断成两个链表
    // 翻转后半段
    let head2 = null
    while (slow) {
        [slow.next, slow, head2] = [head2, slow.next, slow]
    }
    while (head && head2) {
        if (head.val !== head2.val) {
            return false
        }
        head = head.next
        head2 = head2.next
    }
    return true

}

关于这个反转的代码不理解,能不能解释一下

写回答

1回答

lewis

2021-12-15

这是一种js语法,就是交换两个变量的值

0
5
lewis
支持多个变量交换的
2021-12-15
共5条回复

JavaScript版数据结构与算法 轻松解决前端算法面试

夯实算法基础,填补技术短板,助力面试考题最后一公里

2479 学习 · 683 问题

查看课程