Sword finger offer-16-merges two sorted lists

Merge two sorted linked lists

  • Title: input two monotonically increasing linked lists, and output two synthesized linked lists. Of course, we need the synthesized linked list to meet the monotonically non decreasing rule.
  • Train of thought:
    1. Non recursive version: two auxiliary variables are used, list3 and cur. List3 is the head node of the linked list, and cur is the last node added at present. Connect the small nodes of list1.val and list2.val to the back of cur, and move the corresponding list variable one bit later.
    2. Recursive version: get the head nodes with smaller values of two linked lists as the head nodes of the combined linked list. Then continue to merge the remaining nodes in the two linked lists, and link the nodes with smaller values to the merged linked list.
  • Boundary:
    1. Non recursion: the loop condition is that neither list1 nor list2 is null. When one is null, connect the other directly after cur.
    2. Recursion: when list1 is null, list2 is returned; when list2 is null, list1 is returned.
  • Nonrecursive code
/*
public class ListNode {
    int val;
    ListNode next = null;
 
    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode Merge(ListNode list1,ListNode list2) {
        ListNode list3 = null;
        ListNode cur = null;
        if(list1==null){
            return list2;
        }
        if(list2==null){
            return list1;
        }
        while(list1!=null&&list2!=null){
            if(list1.val<=list2.val){
            	//Set the head node first when the head node is empty
                if(list3==null){
                    cur = list3 = list1;
                }else{
                    cur.next = list1;
                    cur = cur.next;
                }
                list1 = list1.next;
            }else{
            	//Set the head node first when the head node is empty
                if(list3==null){
                    cur = list3 = list2;
                }else{
                    cur.next = list2;
                    cur = cur.next;
                }
                list2 = list2.next;
            }
        }
        if(list1==null){
            cur.next = list2;
        }
        if(list2==null){
            cur.next = list1;
         }
        return list3;
    }
}
  • Recursive code
/*
public class ListNode {
    int val;
    ListNode next = null;
 
    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode Merge(ListNode list1,ListNode list2) {
        ListNode list3 = null;
        ListNode cur = null;
        if(list1==null){
            return list2;
        }
        if(list2==null){
            return list1;
        }
        ListNode res = null;
        if(list1.val<=list2.val){
            res = list1;
            list1.next = Merge(list1.next,list2);
        }else{
            res = list2;
            list2.next = Merge(list1,list2.next);
        }
        return res;
    }
}

Posted by Zjoske on Mon, 21 Oct 2019 12:44:45 -0700