92. Reverse Linked List II
class Solution {
public:
ListNode* reverseBetween(ListNode* head, int m, int n) {
if(head == NULL || m == n){
return head;
}
ListNode *dummy = new ListNode(0);
ListNode *cur = head;
ListNode *temp = head->next;
for(int i=1;i<m;i++){
dummy = cur;
cur = temp;
temp = temp->next;
}
ListNode *dummy2 = dummy;
ListNode *dummym = cur;
for(int i=m; i<n;i++){
cur->next = dummy;
dummy = cur;
cur = temp;
temp = temp->next;
}
cur->next = dummy;
dummy2->next = cur;
dummym->next = temp;
if(m == 1)
return dummy2->next;
else
return head;
}
};