My C++ iterative solution


  • 2
    L
    ListNode *reverseList(ListNode *head)
    {
    	if (head == NULL || head->next == NULL)
    		return head;
    
    	ListNode *pCurr = head;
    	ListNode *pPrev = NULL;
    	ListNode *pNext = NULL;
    
    	while (pCurr != NULL)
    	{
    		pNext = pCurr->next;  //save next node
    		pCurr->next = pPrev;
    		if (pNext == NULL)
    			head = pCurr;
    		pPrev = pCurr;
    		pCurr = pNext;
    	}
    
    	return head;
    }

Log in to reply
 

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