用的栈,懒得重新建链表了
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
stack<int>st;
int i = 1;
ListNode* p;
ListNode* yuan = head;
//read
while (head&&i<=n) {
if (i == m) {
p= head;
}
if (i >= m) {
st.push(head->val);
}
head = head->next;
i++;
}
//put
i = m;
while (i <= n) {
int temp = st.top();
if (!st.empty())
st.pop();
p->val = temp;
p = p->next;
i++;
}
return yuan;
}
};

京公网安备 11010502036488号