Sharing easy understanding my c++ code


  • 0
    S
      if (l1 == NULL)
    		return l2;
        if (l2 == NULL) 
    		return l1;
    	ListNode  temp(0);
    	ListNode* result = &temp;
    	    while (1){	//完全靠break跳出		
    	if (l1->val<l2->val) {				
    			result->next = l1;
    			result = l1; //非常重要
    			if (l1->next == NULL){//判断该链表是否到头了,如果到头,另一个直接贴在后面
    				result->next = l2;
    				break;}
    			else
                    l1 = l1->next;
    			
    		} else {
    			result->next = l2;
    			result = l2;
               if (l2->next == NULL){
    				result->next = l1;
    				break;}
    			else
                    l2 = l2->next;
    		}
    	}
    	return temp.next;

Log in to reply
 

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