/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseBetween(ListNode* head, int m, int n) {
if (head == NULL)
return head;
ListNode *voidptr = new ListNode(0);
voidptr->next = head;
ListNode *m_node = voidptr;
ListNode *n_node = voidptr;
ListNode *cur_node = head;
ListNode *first;
ListNode *last;
int index = 1;
while(index <= n && cur_node != NULL){
if (index == m - 1)
m_node = cur_node;
if (index == n)
n_node = cur_node;
if (m <= index && index <= n){
int val = cur_node->val;
ListNode* tmp = new ListNode(val);
if (m == index)
first = tmp, last = first;
else
tmp->next = last, last = tmp;
}
cur_node = cur_node->next;
++index;
}
m_node->next = last;
first->next = n_node->next;
return voidptr->next;
}
};
static int x=[](){
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return 0;
}();
LetCode 92. 反转链表 II
最新推荐文章于 2025-10-12 07:00:00 发布
本文介绍了一种反转链表中指定区间[m, n]的方法,并提供了一个C++实现示例。该方法通过创建辅助节点和使用迭代的方式实现了链表区间的反转。
367

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



