/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int val=0, ListNode next=null) {
* this.val = val;
* this.next = next;
* }
* }
*/
public class Solution {
public ListNode ReverseBetween(ListNode head, int left, int right) {
var dummy = new ListNode(0, head);
var prev = dummy;
for (int i = 1; i < left; i++)
prev = prev.next; // prev = node just before the reversal zone
var curr = prev.next; // curr = first node of the zone; it ends up as the zone's tail
for (int i = 0; i < right - left; i++) {
var next = curr.next; // node to move to the front of the zone
curr.next = next.next; // unlink it
next.next = prev.next; // point it at the current zone front
prev.next = next; // make it the new zone front
}
return dummy.next;
}
}It’s tricky to implement.