Reverse Linked List II java

时间:2022-10-22 09:39:33
 public static ListNode reverseBetween(ListNode head, int m, int n) {
ListNode pre=head,current=head,mPre = new ListNode(0),mNode = new ListNode(0),nNode = new ListNode(0),temp;
mPre.next=head;
int i=1;
while(i<=n)
{
if(i==m-1)
mPre=current;
if(i==m)
mNode=current;
if(i==n)
nNode=current;
if(m<i&&i<=n)
{
temp=current;
current=current.next;
temp.next=pre;
pre=temp;
}
else{
pre=current;
current=current.next;
}
i++;
}
mPre.next=nNode;
mNode.next=current;
if(m==1)
return nNode;
else
return head; }