力扣链接:https://leetcode.cn/problems/LGjMqU/description/
/*** Definition for singly-linked list.* function ListNode(val, next) {* this.val = (val===undefined ? 0 : val)* this.next = (next===undefined ? null : next)* }*/
/*** @param {ListNode} head* @return {void} Do not return anything, modify head in-place instead.*/
// 算法:将链表一分为2,将第二段链表反转,然后第一段链表和第二段链表交叉合并
var reorderList = function (head) {// 双指针快慢法寻找中间节点let slow = headlet fast = headwhile (fast.next && fast.next.next) {slow = slow.nextfast = fast.next.next}// 将中间节点的下一节点作为第二段节点的头节点let scondList = slow.next//断开与第二条链表的链接slow.next = null// 反转第二条链表let pre = nulllet cur = scondListwhile (cur) {let temp = cur.nextcur.next = prepre = curcur = temp}// 更新第二条链表scondList = pre//合并链表let first = headlet second = scondListwhile (second) {const temp1 = first.nextconst temp2 = second.nextfirst.next = second;second.next = temp1first = temp1second = temp2}
};