ListNode *reverseList(ListNode *p, int m, int n, int current,
ListNode **mp0, ListNode **mp, ListNode **np, ListNode **np1) {
if (m-1 < 1) {
*mp0 = NULL;
} else if (current == m-1) {
*mp0 = p;
}
if (current == m) {
*mp = p;
}
if (current == n) {
*np1 = p->next;
*np = p;
return p;
}
ListNode *nx = reverseList(p->next, m, n, current+1, mp0, mp, np, np1);
if (current >= m && current < n) {
nx->next = p;
}
return p;
}
ListNode *reverseBetween(ListNode *head, int m, int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
ListNode *mp0, *mp, *np, *np1;
reverseList(head, m, n, 1, &mp0, &mp, &np, &np1);
if (mp0 == NULL) {
head = np;
} else {
mp0->next = np;
}
mp->next = np1;
return head;
}
answered
14 Apr, 21:53
whitebluff
21●2
accept rate:
0%