C++(11ms) AC clean solution


  • 0
    K
      ListNode *partition(ListNode *head, int x) {
    	 if (head == NULL)return NULL;
    	 ListNode* FisrtTempNodeLessThanXAndGx = NULL;
    	 ListNode* SecondTempNodeLessThanXAndGx = NULL;
    	 ListNode* NodeTravel = head;
    	 ListNode* ParentNodeTravel = NULL;
    	 ListNode* NewHead = head;
    	 while (NodeTravel)
    	 {
    		 if (NodeTravel->val < x)
    		 {
    			 SecondTempNodeLessThanXAndGx = NodeTravel;
    			 if (FisrtTempNodeLessThanXAndGx != NULL)
    			 {
    
    				 ParentNodeTravel->next = SecondTempNodeLessThanXAndGx->next;
    				 SecondTempNodeLessThanXAndGx->next = FisrtTempNodeLessThanXAndGx->next;
    				 FisrtTempNodeLessThanXAndGx->next = SecondTempNodeLessThanXAndGx;
    			 }
    			 else
    			 {
    				 if (ParentNodeTravel)
    				 {
    					 ParentNodeTravel->next = SecondTempNodeLessThanXAndGx->next;
    					 SecondTempNodeLessThanXAndGx->next = head;
    					 NewHead = SecondTempNodeLessThanXAndGx;
    				 }
    			 }
    		 }
    		 ParentNodeTravel = NodeTravel;
    		 FisrtTempNodeLessThanXAndGx = SecondTempNodeLessThanXAndGx;
    		 NodeTravel = NodeTravel->next;
    	 }
    	 return NewHead;
     }

Log in to reply
 

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