/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* reverseBetween(ListNode* head, int left, int right) {
ListNode *DummyNode = new ListNode;
DummyNode ->next = head;
ListNode *LeftList = DummyNode;
for(int i =0;i<left-1;i++) LeftList = LeftList->next;
ListNode *LeftNode = LeftList->next;
ListNode *RightNode = LeftNode;
for(int i = 0;i<right-left;i++) RightNode = RightNode->next;
ListNode *RightList = RightNode->next;
RightNode->next=NULL;
LeftList->next = NULL;
ListNode*pre,*temp = NULL;
ListNode*cur = LeftNode;
while(cur)
{
temp = cur->next;
cur->next = pre;
pre = cur;
cur = temp;
}
LeftList->next = pre;
LeftNode->next = RightList;
return DummyNode->next;
}
};
92. 反转链表 II
于 2022-12-22 01:17:56 首次发布