[LeetCode-Java Exercise] 21. Merge two ordered linked lists (simple)

1. Title description

Insert picture description here

2. Problem solving ideas

Idea 1: Recursion
We can recursively define the merge operation in two linked lists as follows (ignoring boundary conditions, such as empty linked lists, etc.):

list1[0] + merge(list1[1:], list2) list1[0] < list2[0]
list2[0] + merge(list1, list2[1:]) otherwise

In other words, a node with the smaller head value of the two linked lists is merged with the result of the merge operation of the remaining elements.
Algorithm
We directly model the above recursive process, and at the same time need to consider the boundary conditions.

If l1 or l2 is an empty linked list from the beginning, then there is no operation to merge, so we only need to return a non-empty linked list. Otherwise, we have to determine which of l1 and l2 the head node of the linked list has the smaller value, and then recursively decide the next node to be added to the result. If one of the two linked lists is empty, the recursion ends.

Idea 2: Iteration
We can use iterative methods to implement the above algorithm. When both l1 and l2 are not empty linked lists, judge which of l1 and l2 has the lower value of the head node of the linked list, and add the node with the smaller value to the result. When a node is added to the result, it will correspond to the linked list The node of moves back one bit.

Algorithm
First, we set a sentinel node prehead, which can make it easier for us to return to the merged linked list at the end. We maintain a prev pointer, and what we need to do is adjust its next pointer. Then, we repeat the following process until l1 or l2 points to null: If the value of the current node of l1 is less than or equal to l2, we connect the current node of l1 to the back of the prev node and move the l1 pointer one bit backward. Otherwise, we do the same for l2. Regardless of which element we follow, we need to move prev one bit backward.

At the end of the loop, at most one of l1 and l2 is non-empty. Since the two input linked lists are in order, no matter which linked list is non-empty, all the elements it contains are larger than all the elements in the previously merged linked list. This means that we only need to simply connect the non-empty linked list to the back of the merged linked list and return to the merged linked list.

3. Code implementation

Idea one code:

class Solution {
    
    
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
    
    
        if (l1 == null) {
    
    
            return l2;
        } else if (l2 == null) {
    
    
            return l1;
        } else if (l1.val < l2.val) {
    
    
            l1.next = mergeTwoLists(l1.next, l2);
            return l1;
        } else {
    
    
            l2.next = mergeTwoLists(l1, l2.next);
            return l2;
        }

    }
}

Idea two code:

class Solution {
    
    
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
    
    
        ListNode prehead = new ListNode(-1);

        ListNode prev = prehead;
        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;
        }

        // 合并后 l1 和 l2 最多只有一个还未被合并完,我们直接将链表末尾指向未合并完的链表即可
        prev.next = l1 == null ? l2 : l1;

        return prehead.next;
    }
}

Guess you like

Origin blog.csdn.net/weixin_48683410/article/details/113616181