Sword refers to offer25 Merge two ordered linked lists

Sword refers to offer25 Merge two ordered linked lists

Method 1: Recursion

Idea: We can recursively define the merge operation in two linked lists as follows (ignoring edge cases, such as empty linked lists, etc.): {
list ⁡ 1 [ 0 ] + merge ⁡ ( list 1 [ 1 : ] , list 2 ) list 1 [ 0 ] < list ⁡ 2 [ 0 ] list 2 [ 0 ] + merge ( list 1 , list 2 [ 1 : ] ) otherwise \left\{\begin{array}{ll} \operatorname{list} 1[0]+ \operatorname{merge}(\text { list } 1[1:], \text { list } 2) & \text { list } 1[0]<\operatorname{list} 2[0] \\ \text { list } 2[0]+\text { merge }(\text { list } 1, \text { list } 2[1:]) & \text { otherwise } \end{array}\right.{ list1[0]+merge( list 1[1:], list 2) list 2[0]+ merge ( list 1, list 2[1:]) list 1[0]<list2[0] otherwise 
That is to say, the node with the smaller head value of the two linked lists is merged with the merge operation result of the remaining elements.

algorithm

We model the above recursive process directly, and we need to consider boundary cases.

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

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;
        }
    }
}

The recursive method code is highly readable and easy to remember and understand.

Method 2: Iteration

Idea: We can use iterative methods to implement the above algorithm. When neither l1 nor l2 is an empty linked list, determine which linked list's head node has a smaller value, and add the node with the smaller value to the result. When a node is added to the result, the corresponding linked list The node in is moved one position backward.

algorithm

1. We set a sentinel node prehead, which allows us to return the merged linked list more easily at the end.

2. 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 position backward. Otherwise, we do the same for l2. No matter which element we connect to the back, we need to move prev one position backward.

When the loop terminates, at least one of l1 and l2 is non-empty. Since the two input linked lists are both ordered, no matter which linked list is non-empty, all the elements it contains are more rounded than all the previously merged linked lists. This means we simply append the non-empty linked list to the merged list and return the merged list.

references

[1] https://leetcode.cn/problems/merge-two-sorted-lists/solution/he-bing-liang-ge-you-xu-lian-biao-by-leetcode-solu/

Guess you like

Origin blog.csdn.net/ChenglinBen/article/details/131212544