Sword finger offer: merging two sorted linked lists

Keywords: Java network

1. Problem description

Input two monotonically increasing linked lists, and output two synthesized linked lists. Of course, we need the synthesized linked lists to meet the monotone rule.

2. way of thinking

Method 1: non recursive method

According to the problem, which is very similar to the sorting out process, two arrays are sorted separately, and then they are sorted as a whole. So the idea of this problem is very simple, that is, two variables are used to traverse two linked lists respectively. Compare the values of the two nodes traversed, the small node is disconnected from the later connection, connected to the other node traversed, and let the small node move backward one bit at the same time. The location of the large node remains the same, so a variable is needed to record the next node after the node with smaller value, so as to ensure that the later node can be obtained after disconnection of the connection with the later node. In this way, loop operation is carried out until the last stop of a link list cycle. The two link lists will be combined into an orderly new link list.

/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode Merge(ListNode list1,ListNode list2) {
        if(list1 == null)    //Determine whether list 1 is null. If it is null, return to list 2 directly.
            return list2;
        else if(list2 == null)    //Determine whether the link list 2 is null.
            return list1; 
        ListNode pNode1 = list1;    //Record the head node of linked list 1. Since you want to return a new linked list, compare the head node with a smaller return value.
        ListNode pNode2 = list2;    //Record the head node of linked list 2  
        ListNode next = null;    //When the node is disconnected from the later node, it is used to record a node behind the small node to ensure that it can traverse to.
        while(list1 != null && list2 != null){    //As long as there is a linked list to the end of the cycle stop
            if(list1.val <= list2.val){    //Smaller nodes connect to larger nodes
                next = list1.next;
                list1.next = list2;
                list1 = next;
            } else{
                next = list2.next;
                list2.next = list1;
                list2 = next;
            }
        }
        return pNode1.val <= pNode2.val ? pNode1 : pNode2;    //Returns the smaller of the two head nodes.
    }
}

Method 2: recursive method

The key is to determine the val value of list1 and list2, and then change the next direction of list1 or list2.

/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode Merge(ListNode list1,ListNode list2) {
        if(list1 == null){
            return list2;
        }else if(list2 == null){
            return list1;
        }
        if(list1.val <= list2.val){
            list1.next = Merge(list1.next,list2);    
            //Here is to change the next direction of LIST1, which is filled with list1.next to move the LIST1 node backward one bit.
            return list1;
        }else{
            list2.next = Merge(list2.next,list1);
            return list2;
        }
    }
}

Refer to Niuke network for recursion method: Uncle pizza's method

Posted by imderek on Sun, 20 Oct 2019 14:46:53 -0700