题解 | #链表内指定区间反转#
链表内指定区间反转
https://www.nowcoder.com/practice/b58434e200a648c589ca2063f1faf58c
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
#include <list>
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* pre = new ListNode(0);
ListNode* cur = head;
ListNode* left = nullptr;
ListNode* right = nullptr;
ListNode* temp = nullptr;
pre->next = cur;
int i;
if(head == nullptr)
return nullptr;
if(m == n)
return head;
if(m>n)
return nullptr;
for(i=1;i<m;i++)
{
pre = cur;
cur = cur->next;
}
left = cur;
right = left ->next;
for(i=m;i<n;i++)
{
temp = right->next;
right->next = left;
left = right;
right = temp;
}
pre->next = left;
cur->next = right;
if(m>1)
{
return head;
}
else {
return left;
}
}
};
顺丰集团工作强度 409人发布
查看20道真题和解析