Simple Java solution


  • 0
    M
    ListNode tmp, p1 = new ListNode(-1);
    	 		p1.next = l1; l1 = p1;
    	 		while(p1.next != null && l2 != null){
    	 			if(p1.next.val > l2.val){
    	 				tmp = l2.next;
    	 				l2.next = p1.next;
    	 				p1.next = l2;
    	 				l2 = tmp;
    	 			}else p1 = p1.next;
    	 		}
    	 		if(p1.next == null && l2 != null)
    	 			p1.next = l2;
    	 		return l1.next;

Log in to reply
 

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