In this Leetcode Reverse Linked List II problem solution we have Given the head of a singly linked list and two integers left and right where left <= right, reverse the nodes of the list from the position left to position right, and return the reversed list.

## Problem solution in Python.

```class ListNode:
def __init__(self, x):
self.val = x
self.next = None

class Solution:
def reverseBetween(self, head: ListNode, m: int, n: int) -> ListNode:
counts = 0
stack = []
dummy = ListNode(0)
pre = dummy
counts += 1
if counts < m:
pre = pre.next
elif counts >=m and counts <=n:
else:
break

while stack:
pre.next = stack.pop()
pre = pre.next

return dummy.next
```

## Problem solution in Java.

```public ListNode reverseBetween(ListNode head, int m, int n) {
ListNode preRev = null;
int i = 1;
while(i<m) { preRev = curr; curr = curr.next; i++; }

ListNode end = curr;
ListNode pre = null;
while(i<=n) {
ListNode temp = curr.next;
curr.next = pre;
pre = curr;
curr = temp;
i++;
}

end.next = curr;
if(preRev != null) preRev.next = pre;
return preRev == null ? pre: head;
}
```

## Problem solution in C++.

```ListNode* reverseBetween(ListNode* head, int m, int n) {
for(int i = 0; i < m-2; i++) l = l->next;

ListNode* pre = NULL;
ListNode* cur = m == 1 ? l : l->next;
ListNode* next = cur->next;
for(int i = 0; i < n-m+1; i++){
cur->next = pre;
pre = cur;
cur = next;
next = next ? next->next : NULL;
}
if(m != 1) l->next = pre;
return m == 1 ? pre : head;
}
```

## Problem solution in C.

```struct ListNode* reverseBetween(struct ListNode* head, int m, int n) {
return tmp;
}
struct ListNode* revtail;
struct ListNode* before = NULL;
int counter = 0;
while(counter++ < m - 1){
before  = scanner;
scanner = scanner->next;
}

before = scanner;
struct ListNode* curr = scanner->next;
struct ListNode* curr_next = NULL;

revtail = scanner;
counter = 0;
while(counter++<(n - m)){
curr_next = curr->next;
curr->next = before;
before = curr;
curr = curr_next;
}