NC2 重排链表
//1,2,3,4,5
//1,5,2,4,3
public class Solution {
public ListNode reverse(ListNode head){
ListNode prev=null;
ListNode cur=head;
while(cur!=null){
ListNode next=cur.next;
cur.next=prev;
prev=cur;
cur=next;
}
return prev;
}
public void reorderList(ListNode head) {
if(head==null||head.next==null){
return;
}
ListNode slow=head;
ListNode fast=head;
while(fast!=null&&fast.next!=null){
slow=slow.next;
fast=fast.next.next;
}
ListNode vhead=slow.next;
slow.next=null;
vhead=reverse(vhead);
while(vhead!=null){
ListNode cur=vhead.next;
vhead.next=head.next;
head.next=vhead;
head=vhead.next;
vhead=cur;
}
}
}