链表指定区间内反转
此题的解法为 在需要反转的区间里,每遍历到一个节点,让这个新节点来到反转部分的起始位置。 所以需要记住他的这个图即可。 中间指向最后一个,后边依次递推,即。 ListNode tail=h.next; h.next=tail.next; tail.next=p.next; p.next=tail;
// write code here
ListNode phead=new ListNode(0);
phead.next=head;
ListNode p=phead;
ListNode h=head;
for (int i=1;i<m;i++){
p=h;
h=h.next;
}
for (int i=0;i<n-m;i++){
ListNode tail=h.next;
h.next=tail.next;
tail.next=p.next;
p.next=tail;
}
return phead.next;
}