四指针法解决重排链表问题
/**
* Definition for singly-linked list.* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public void reorderList(ListNode head) {
if(head==null)
return ;
ListNode p1=head;
ListNode p2=head;
ListNode p3=head;
ListNode p4=head;
while(p4.next!=null){
p4=p4.next;
}
while(p1!=p4&&p1.next!=p4){
p2=p1.next;
p3=p1;
while(p3.next!=p4){
p3=p3.next;
}
p1.next=p4;
p4.next=p2;
p1=p2;
p4=p3;
}
p4.next=null;
}
}