反转链表Ⅱ

class Solution{
public ListNode reverseBetween(ListNode head, int left, int right) {
if(left == right || head == null) return head;
ListNode initialHead = head;
ListNode start = head;
ListNode end = null;
Stack<ListNode> stack = new Stack<>();
int location = 1;
while(head != null){
if(location < left){
start = head;
}else{
if(location <= right){
stack.push(head);
}else{
end = head;
break;
}
}
head = head.next;
location++;
}
ListNode reverseStart = stack.peek();
ListNode cur = stack.pop();
while(!stack.isEmpty()){
cur.next = stack.peek();
cur = stack.pop();
}
cur.next = end;
if(left == 1) return reverseStart;
start.next = reverseStart;
return initialHead;
}
}