Hi. I'm getting a runtime error for this with Last executed input: {5}, 1, 1
The code is not great, but i've tested it on my machine and it appears to work. Moreover, because of the very first if
condition, I have no idea how that test case could fail :S
public ListNode reverseBetween(ListNode head, int m, int n) {
if(head == null || m == n) return head;
ListNode head_ = head; // save old head
for(int i = 1; i < m-1; i++) head = head.next;
ListNode start = head;
ListNode revEnd = m == 1 ? head : head.next;
ListNode prev = revEnd;
head = prev;
for(int i = m; i <= n; i++){
ListNode next = head.next;
head.next = prev;
prev = head;
head = next;
}
// Thread appropriately
revEnd.next = head;
if(m == 1) head_ = prev; // if we need to replace the head
else start.next = prev;
return head_;
}
Anyone see any problems? I'm surprised since it shouldn't get any farther than the first if statement. Thanks.