题目
解题思路
情况一:链表为空,直接返回头节点即可
情况二:链表不为空时
总代码
class Solution {
public ListNode reverseBetween(ListNode head, int left, int right) {
if(head==null){
return head;
}
ListNode newHead=new ListNode();
newHead.next=head;
ListNode pre=newHead;
for(int i=1;i<left;i++){
pre=pre.next;
}
ListNode cur=pre.next;
for(int i=left;i<right;i++){
ListNode curNext=cur.next;
cur.next=curNext.next;
curNext.next=pre.next;
pre.next=curNext;
}
return newHead.next;
}
}