My mergeKLists using merge of two lists and a queue.


  • 0
    N
    node *mergeKLists(vector<node *> &lists)
    {
    	std::queue <node *> q;
    	for (vector<node *>::iterator it = lists.begin(); it != lists.end(); ++it) {
    		q.push(*it);
    	}
    
    	node* l1 = 0; 
    	node* l2 = 0;  
    	while (!q.empty()) {
    		l1 = q.front();
    		q.pop();
    		if (q.empty()) {
    			break;
    		}
    		else {
    			l2 = q.front();  q.pop();
    		}
    			
    		q.push (mergeTwoLists(l1, l2));
    	}
    
    	return l1;
    }

Log in to reply
 

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