反转链表 II
回顾知识解题思路class="superseo">链表反转,有时完全用不到链表 *** 作
数据很少,可以开数组直接水过(感觉被英雄哥带偏,但很爽快)
代码详解//水代码
class Solution {
public:
ListNode* reverseBetween(ListNode* head, int left, int right) {
int a[right + 1];
int l = left, r = right;
int i = 1;
ListNode* h=nullptr,*p=nullptr;
h=head;p = head;
while(i != left) {
head = head->next;
i++;
}
h = head;
while (l <= r) {
a[l] = head->val;
head = head->next;
l++;
}
//cout << a[left]<<" "<
//p = h;
while(left <= r) {
h->val = a[r];
r--;
h = h->next;
}
return p;
}
};
【过啦】!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)