/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* fanzhuan(ListNode* pre, ListNode* last)
{
ListNode* lpre = pre->next;
ListNode* cur = lpre->next;
while (cur != last)
{
lpre->next = cur->next;//这个lpre是不变的
cur->next = pre->next;
pre->next = cur;
cur = lpre->next;
}
return pre->next;//注意返回值是什么
}
ListNode* reverseBetween(ListNode* head, int m, int n) {
if(m==n)
return head;
ListNode *dummy=new ListNode(0);
dummy->next=head;
ListNode *cur=dummy;
ListNode *pre=head;
int c=1;
while(cur)
{
if(c==m)
pre=cur;
if(c==n+1)
{
pre->next=fanzhuan(pre,cur->next);//涉及边界问题 最好从哑结点开始
}
c++;
cur=cur->next;
}
return dummy->next;
}
};
92. 反转链表 II
最新推荐文章于 2025-03-13 21:30:47 发布
865

被折叠的 条评论
为什么被折叠?



