leetCode.92. 反转链表 II
题目思路
代码
/*** 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;}
};