c++ straight foward solution


  • 0
    B
    /**
     * 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(m==n) return head;
            int i=0;
            ListNode *dummy=new ListNode(0);
            dummy->next=head;
            ListNode *p=dummy;
            
            while(++i<m && p->next){
                p=p->next;
            }
            ListNode *tail=p->next;
            head=p;
            p=p->next;
            
            ListNode *r=p, *cur=p->next, *t;
            
            while(i++<n && cur){
                t=cur->next;
                cur->next=r;
                r=cur;
                cur=t;
                
                p=p->next;
            }
        
            head->next=r;
            tail->next=t;
            return dummy->next;
        }
    };
    

Log in to reply
 

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