# Took 4ms.. in C++ .. Can we improve better?

• ``````/**
* Definition for singly-linked list.
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *reverseBetween(ListNode *head, int m, int n) {
int count = 1 ;
ListNode *temp = NULL , *nextNode = NULL , *current = head , *leftm = NULL;

if(m == n)

while(count <= n){
if(count >= m && count <= n ){
nextNode = current->next;
current->next = temp;
temp = current;
current = nextNode;
} else {
if(count == m-1)
leftm = current;
current = current->next;
}

count++;
}

if(leftm){
leftm->next->next = current;
leftm->next = temp;
} else {
return temp;
}

}
};
``````

My solution is O(n)

• Mine also took 4ms. Just a little bit shorter:

``````class Solution {
public:
ListNode *reverseBetween(ListNode *head, int m, int n) {
ListNode * oper0 = head, * oper1 = head, * oper2 = head;
for (int i =0; i < m-1; i++) {
}
oper1 = oper0;
oper2 = oper1->next;
for( int i = 0; i < n-m; i++) {
oper0 = oper1;
oper1 = oper2;
oper2 = oper2->next;
oper1->next = oper0;
}