Given the head of a singly linked list and two integers left and right where left &lt;= right, reverse the nodes of the list from position left to position right, and return the reversed list.

&nbsp;
Example 1:

Input: head = [1,2,3,4,5], left = 2, right = 4
Output: [1,4,3,2,5]


Example 2:

Input: head = [5], left = 1, right = 1
Output: [5]


&nbsp;
Constraints:


	The number of nodes in the list is n.
	1 &lt;= n &lt;= 500
	-500 &lt;= Node.val &lt;= 500
	1 &lt;= left &lt;= right &lt;= n


&nbsp;
Follow up: Could you do it in one pass?
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def reverseBetween(self, head: Optional[ListNode], left: int, right: int) -> Optional[ListNode]:
        # revrese api
        def reverse(start, end):
            prev = None
            cur = start
            while prev != end:
                nextNode = cur.next
                cur.next = prev
                prev = cur
                cur = nextNode
            return prev
        
        if not head or not head.next or right <= left:
            return head
        
        start, end = head, head
        node, afterRight = 0, 0
        
        # At the begining
        if left == 1:
            # start index 
            inc = left-1
            while inc > 0:
                start = start.next
                inc -= 1
            # end index
            inc = right-1
            while inc > 0:
                end = end.next
                inc -= 1
            afterRight = end.next
            reverse(start, end)
            head = end
        else: # Left other then begining
            # start index 
            inc = left-2
            while inc > 0:
                start = start.next
                inc -= 1
            # end index
            inc = right-1
            while inc > 0:
                end = end.next
                inc -= 1
            afterRight = end.next
            begin = start # node before left
            start = start.next
            reverse(start, end)
            begin.next = end
            
        # If their's ll chain left agter right chain it to the updated ll
        if afterRight:
            start.next = afterRight

        return head
    
"""
TC : O(n)
Sc : O(1)

"""
class Solution {
    public ListNode reverseBetween(ListNode head, int left, int right) {
        if(left==right) return head;
        ListNode last = null;
        ListNode present = head;
        
        for(int i=0; present != null && i<left-1;i++){
            last = present;
            present = present.next;
        }
        ListNode finalEnd = present;
        
        ListNode prev = last;
        ListNode next = present.next;
        
        for(int i=0; present != null && i<right-left+1;i++){
            present.next = prev;
            prev = present;
            present = next;
            if(next!= null){
                next = next.next;
            }
        }
        if(last != null){
            last.next = prev;
        }
        else{
            head = prev;
        } 
        finalEnd.next = present;
        return head;
    }
}
/**
 * Definition for singly-linked list.
 * struct ListNode {
 * int val;
 * ListNode *next;
 * ListNode() : val(0), next(nullptr) {}
 * ListNode(int x) : val(x), next(nullptr) {}
 * ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:

    ListNode* reverse(ListNode* head)
    {
        ListNode* prev = NULL;
        while(head != NULL)
        {
            ListNode* temp = head->next;
            head->next = prev;
            prev = head;
            head = temp;
        }
        return prev;
    }

    ListNode* reverseN(ListNode* head,int right)
    {
        ListNode* temp = head;
        while(right != 1)
            temp = temp->next,right--;

        ListNode* temp2 = temp->next;
        temp->next = NULL;

        ListNode* ans = reverse(head);
        head->next = temp2;
        return ans;

    }

    ListNode* reverseBetween(ListNode* head, int left, int right) {

          if(left == right)
              return head;

          if(left == 1)
          {
              return reverseN(head,right);
          }
          else
          {
              ListNode* temp = head;
              right--;
              while(left != 2)
              {
                  temp = temp->next;
                  left--;
                  right--;
              }
              ListNode* temp2 = reverseN(temp->next,right);
              temp->next = temp2;
              return head;
          }

         return NULL;

    }
};
/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @param {number} left
 * @param {number} right
 * @return {ListNode}
 */

var reverseBetween = function(head, left, right) {
    
    if (!head || !head.next || left === right) {
        
        return head;
    }
        
        let dummyHead = new ListNode(-1);

        dummyHead.next = head;

        let prev = dummyHead;

        for (let i = 1; i < left; i++) {
            
            prev = prev.next;
        }

        let curr = prev.next;

        for (let i = left; i < right; i++) {

            let next = curr.next;
            curr.next = next.next;
            next.next = prev.next;
            prev.next = next;
        }
        
        return dummyHead.next;
};
Reverse Linked List II