1) 怎么写reverse linkedlist 逻辑图 http://www.geeksforgeeks.org/write-a-function-to-reverse-the-nodes-of-a-linked-list/
2) 注意first half和second half 都要有null 结尾
ref http://www.cnblogs.com/springfor/p/3869333.html
public class Solution {
public void reorderList(ListNode head) {
if(head==null || head.next == null) return;
ListNode run = head, walk = head, cur = head;
while(run.next!=null && run.next.next!=null){
run = run.next.next;
walk = walk.next;
}
ListNode half = walk.next;
walk.next = null; //没加这个会TLE
half = reverse(half);
while(half!=null){
ListNode next = cur.next;
ListNode next2 = half.next;
cur.next = half;
half.next = next;
//cur = half.next;
cur=next;
half = next2;
}
}
public ListNode reverse(ListNode head){
if(head==null || head.next ==null) return head;
ListNode pre=head;
ListNode cur = head.next;
while(cur!=null){
ListNode next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
head.next = null;
return pre;
}
}