题目:
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}
.
题解:
题目要重新按照 L0→Ln→L1→Ln-1→L2→Ln-2→…来排列,看例子1->2->3->4会变成1->4->2->3,拆开来看,是{1,2}和{4,3}的组合,而{4,3}是{3,4}的逆序。这样问题的解法就出来了。
第一步,将链表分为两部分。
第二步,将第二部分链表逆序。
第三步,将链表重新组合。
代码如下:
Reference://http://www.programcreek.com/2013/12/in-place-reorder-a-singly-linked-list-in-java/
Reorder List leetcode java,布布扣,bubuko.com
原文:http://www.cnblogs.com/springfor/p/3869333.html