Java solution for reference


  • 40
    C

    Similar to array, the difference is if any of two listnode is not null after first loop, we only need to add it as previous node's next and no need to add them one by one.

    public class Solution {
        public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
            if (l1 == null && l2 == null) {
                return null;
            }
            if (l1 == null) {
                return l2;
            }
            if (l2 == null) {
                return l1;
            }
            ListNode result = new ListNode(0);
            ListNode prev = result;
            while (l1 != null && l2 != null) {
                if (l1.val <= l2.val) {
                    prev.next = l1;
                    l1 = l1.next;
                } else {
                    prev.next = l2;
                    l2 = l2.next;
                }
                prev = prev.next;
            }
            if (l1 != null) {
                prev.next = l1;
            }
            if (l2 != null) {
                prev.next = l2;
            }
            return result.next;
        }
    }

  • 23
    P

    Could use a fake head to avoid edge case.

    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
    	ListNode fakeHead = new ListNode(0);
    	ListNode current = fakeHead;
    
    	while (l1 != null || l2 != null) {
    		if (l1 == null || (l2 != null && l1.val >= l2.val)) {
    			current.next = l2;
    			current = l2;
    			l2 = l2.next;
    		} else {
    			current.next = l1;
    			current = l1;
    			l1 = l1.next;
    		}
    	}
    	return fakeHead.next;
    }

  • 0
    T

    Ideally according to me, the order should also be found out using code as it is not given in the question that sorting is done in ascending order. This can be done easily by comparing a head value of either list that contains at least 2 nodes, with its corresponding head.next value. I think the question is flawed in that sense.


  • 0
    M
            if(l1 == null) return l2;
            if(l2 == null) return l1;
            ListNode tmp,ans;
            if(l1.val <= l2.val){
                tmp = l1;
                l1 = l1.next;
            }else{
                tmp = l2;
                l2 = l2.next;
            }
            ans = tmp;
    
            while(l1 != null && l2 != null){
                if(l1.val <= l2.val){
                    tmp.next = l1;
                    tmp = tmp.next;
                    l1 = l1.next;
                }else{
                    tmp.next = l2;
                    tmp = tmp.next;
                    l2 = l2.next;
                }
            }
            if(l2 == null) tmp.next = l1;
            else tmp.next = l2;
            return ans;
    

  • 3
    E

    We use the same algorithm. BTW, the first if block can be omitted :)


  • 0
    D

    It seems " Sorted Lists " means lists sorted by ascending order default. try [9,3,2,1] [10,7,2]


  • 0
    K

    @peterdyf same code :)


Log in to reply
 

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