9

leetCode解题报告之Reorder List

 3 years ago
source link: https://blog.csdn.net/ljphhj/article/details/21454705
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
leetCode解题报告之Reorder List_快乐de胖虎-CSDN博客

题目:

Given a singly linked list L: L0→L1→…→Ln-1→Ln,
reorder it to: L0→Ln→L1→Ln-1→L2→Ln-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}.

分析:

看到这个题目,我们首先容易想到的就是把链表分成两半,然后把后半部分链表进行逆序一下,之后再和前半部分结合起来就

可以将这道题目AC了!

两个要解决的问题:

1.如何快速的确定链表的中间位置的结点

2.如何将一个链表逆序,并且符合要求“You must do this in-place without altering the nodes' values”

代码实现:


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK