题目介绍
给定一个单链表 L 的头节点 head ,单链表 L 表示为:
L0 → L1 → … → Ln - 1 → Ln 请将其重新排列后变为:
L0 → Ln → L1 → Ln - 1 → L2 → Ln - 2 → … 不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。
示例 1:
输入:head = [1,2,3,4] 输出:[1,4,2,3] 示例 2:
输入:head = [1,2,3,4,5] 输出:[1,5,2,4,3]
提示:
链表的长度范围为 [1, 5 * 104] 1 <= node.val <= 1000
来源:力扣(LeetCode) 链接:leetcode-cn.com/problems/re… 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路
- 快慢指针找到中间节点
- 反转中间节点的后面节点
- 左节点和右节点相互交替值
代码
/**
* 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.
*/
function reverseList(head) {
if (!head) return null
let pre = null
while(head) {
let next = head.next
head.next = pre
pre = head
head = next
}
// console.log(pre)
return pre
}
var reorderList = function(head) {
if (!head || !head.next) return head
let dummy = new ListNode(0, head)
let fast = dummy,
slow = dummy;
while(fast && fast.next) {
fast = fast.next.next
slow = slow.next
}
let temp = slow.next
slow.next = null
let right = reverseList(temp)
while (head && right) {
let headNext = head.next
let rightNext = right.next
head.next = right
right.next = headNext
head = headNext
right = rightNext
}
return dummy.next
};