/**
* 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
最新推荐文章于 2024-07-17 21:13:48 发布