Share my C++ solution using double pointer

  • 0

    Don't need to care about the previous node when using double pointer

    ListNode* reverseBetween(ListNode* head, int m, int n) {
      ListNode **cur = &head;
      while (--n && --m) cur = &(*cur)->next;
      ListNode *pre = *cur;
      while (n > m) {
        ListNode *tmp = *cur;
        *cur = pre->next;
        pre->next = (*cur)->next;
        (*cur)->next = tmp;
      return head;

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.