¥21 链表的合并¥合并两个有序链表

    public ListNode mergeTwoLists5(ListNode l1, ListNode l2) {
        ListNode result = new ListNode();
        ListNode l3 = result;//让L3劳动,最后返回result
        while (l1 != null && l2!= null) {
            if (l1.val < l2.val) {
                ListNode NEW=new ListNode(l1.val);
                l3.next = NEW;
                l3 = NEW;
                l1 = l1.next;
                continue;
            }
            if (l1.val > l2.val) {
                ListNode NEW=new ListNode(l2.val);
                l3.next = NEW;
                l3 = NEW;
                l2=l2.next;
                continue;
            }
            if (l1.val == l2.val) {
                ListNode NEW=new ListNode(l1.val);
                l3.next = NEW;
                l3 = NEW;
                ListNode NEW2=new ListNode(l2.val);
                l3.next = NEW2;
                l3 = NEW2;
                l1 = l1.next;
                l2 = l2.next;
                continue;
            }
        }
        if(l1==null&&l2!=null) {
            while (l2 != null) {
                ListNode NEW = new ListNode(l2.val);
                l3.next = NEW;
                l3 = NEW;
                l2 = l2.next;
            }
        }
        if(l2==null&&l1!=null){
            while (l1!=null){
                ListNode NEW=new ListNode(l1.val);
                l3.next = NEW;
                l3 = NEW;
                l1=l1.next;
            }
        }
        return result.next;
    }
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;
        }

    }
}

作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/merge-two-sorted-lists/solution/he-bing-liang-ge-you-xu-lian-biao-by-leetcode-solu/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

以上是递归版本·····看不懂。。。。。

优化

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    //test1.java
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode result = new ListNode();
        ListNode l3 = result;//让L3劳动,最后返回result
        while (l1 != null && l2!= null) {
            if (l1.val < l2.val) {
                l3.next = l1;
                l1 = l1.next;
                l3=l3.next;
            }
            else {
                l3.next = l2;
                l2=l2.next;
                l3=l3.next;
            }
        }
        if(l1==null&&l2!=null)
                l3.next = l2;
        if(l2==null&&l1!=null)
                l3.next = l1;
        return result.next;

    }
}

猜你喜欢

转载自blog.csdn.net/qq_41557627/article/details/114375050