Skip to content

Files

Latest commit

e4c5165 · Apr 14, 2019

History

History
34 lines (20 loc) · 910 Bytes

链表重排序.md

File metadata and controls

34 lines (20 loc) · 910 Bytes

reorder-list(链表重排序)

知识点:链表

题目描述

Given a singly linked list$ L: L_0→L_1→…→L{n-1}→L_n​$, reorder it to: L 0 L n L 1 L n 1 L 2 L n 2

You must do this in-place without altering the nodes' values.

For example, Given{1,2,3,4}, reorder it to{1,4,2,3}.

给定一个链表$ L: L_0→L_1→…→L{n-1}→L_n$, 将其重排序为: L 0 L n L 1 L n 1 L 2 L n 2 你必须在不改变节点值的情况下完成。 比如: 给定{1,2,3,4},重排序为:{1,4,2,3}

解题思路

比如:$ L: L_0→L_1→…→L{n-1}→L_n​$,首先利用快慢指针将该链表分为两个部分:

L a : L 0 L 1 L n 2 ,

L b : L n 2 + 1 L n 2 + 2 L n

然后将$L_b$翻转,然后合并$L_a$和$L_b$即可。

代码

这里