Reorder List

三步走: 第一步,分成两部分;第二步,将第二部分reverse;第三步,将两个list merge起来。
一定要舍得画图,把指针的指向搞清楚。

public class Solution {
public void reorderList(ListNode head) {
if(head == null) return;
ListNode slow = head, fast = head;
while(fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
}
ListNode second = slow.next;
if(second == null) return;
slow.next = null;
ListNode prev = second, curr = second.next;
while(curr != null) {
ListNode temp = curr.next;
curr.next = prev;
prev = curr;
curr = temp;
}
second.next = null;
second = prev;
ListNode first = head;
while(first != null && second != null) {
ListNode secondNext = second.next;
second.next = first.next;
first.next = second;
first = second.next;
second = secondNext;
}
}
}


posted @ 2014-12-29 06:40  江南第一少  阅读(96)  评论(0)    收藏  举报