class Solution {
public:
/**
*
* @param head ListNode类
* @param m int整型
* @param n int整型
* @return ListNode类
*/
ListNode* reverseBetween(ListNode* head, int m, int n) {
// write code here
ListNode* res = new ListNode(-1);
res->next = head;
ListNode* pre = res;
ListNode* cur = head;
for(int i=1; i<m;i++){
pre = cur;
cur = cur->next;
}
for(int j=m; j<n;j++){
ListNode* temp = cur->next;
cur->next = temp->next;//这儿的cur与pre都是不变的
temp->next = pre->next;
pre->next = temp;
}
return res->next;
}
};
这样一段代码中,reverseBetween函数结束后指针res是否没有释放从而导致内存泄露?如何避免这个问题?感谢。