leetCode.92. 反转链表 II
题目思路
![在这里插入图片描述](/i/ll/?i=direct/7f712c71ff99437bbcefd6c0c72a667f.png)
代码
/**
* 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 m, int n) {
auto dummy = new ListNode( -1 );
dummy->next = head;
auto a = dummy;
for ( int i = 0; i < m - 1; ++ i ) a = a->next;
auto b = a->next;
auto c = b->next;
for ( int i = 0; i < n - m; ++ i ) {
auto t = c->next;
c->next = b;
b = c;
c = t;
}
a->next->next = c;
a->next = b;
return dummy->next;
}
};
标签:II,dummy,ListNode,val,leetCode.92,int,auto,next,链表
From: https://blog.csdn.net/qq_48290779/article/details/140045181