1class Solution {2public ListNode reverseBetween(ListNode head, int left, int right) {3if (left == right) return head;4ListNode last = null;5ListNode present = head;67for (int i = 0; present != null && i < left - 1; i++) {8last = present;9present = present.next;10}11ListNode finalEnd = present;1213ListNode prev = last;14ListNode next = present.next;1516for (int i = 0; present != null && i < right - left + 1; i++) {17present.next = prev;18prev = present;19present = next;20if (next != null) {21next = next.next;22}23}24if (last != null) {25last.next = prev;26} else {27head = prev;28}29finalEnd.next = present;30return head;31}32}