4ms C++ solution


  • 0
    N
        ListNode prehead(0), *l = &prehead, *p = NULL,*q=NULL;
    	int count = 1;
    	while (head)
    	{
    		if (count >= m&&count <= n)//头插法
    		{
    			p = new ListNode(head->val);
    			p->next=q;
    			q = p;
    		}
    		else 	//尾插法
    		{			
    			l->next = new ListNode(head->val);
    			l = l->next;
    		}
    		while(count>=n&&q)
    		{
    			l->next = q;
    			l = l->next;
    			q = q->next;
    		}		
    		head = head->next;
    		count++;
    	}
    	return prehead.next;

Log in to reply
 

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