LeetCode刷题记录

LeetCode 21. 合并两个有序链表

2019-06-27  本文已影响3人  TheKey_

21. 合并两个有序链表

将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。

示例:

输入:1->2->4, 1->3->4
输出:1->1->2->3->4->4

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/merge-two-sorted-lists/
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。


思路:
1.创建一个虚拟头节点 dummyhead, 并维护一个指针 prev
2.如果 l1.val <= l2.val 的话,将 prev.next -> l1, 并将 l1 -> l1.next
3.如果 l2.val > l1.val, 将 prev.next -> l2, 并将 l2 -> l2.next
4.最后判断 l1 是否为空, 如果是则将 prev.next -> l2, 否则 prev.next -> l1;

image.png
public static class ListNode {

        private int val;
        private ListNode next;

        public ListNode(int val) {
            this.val = val;
        }

        //用于测试用例
        public ListNode(int[] arr) {
            if (arr == null || arr.length == 0) throw new NullPointerException("array is Empty");
            this.val = arr[0];
            ListNode cur = this;
            for (int i = 1; i < arr.length; i++) {
                cur.next = new ListNode(arr[i]);
                cur = cur.next;
            }
        }

        @Override
        public String toString() {
            StringBuilder res = new StringBuilder();
            ListNode cur = this;
            while (cur != null) {
                res.append(cur.val + "->");
                cur = cur.next;
            }
            res.append("NULL");
            return res.toString();
        }

    }

    public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        //创建一个虚拟头节点
        ListNode dummyHead = new ListNode(0);
        ListNode prev = dummyHead;

        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;
        }
        prev.next = l1 == null ? l2 : l1;
        return dummyHead.next;
    }

复杂度分析:
时间复杂度:O(m + n), 因为每次循环迭代中,l1 和 l2 只有一个元素会被放进合并链表中,while 循环的次数等于两个链表的总长度。所有其他工作都是常数级别的,所以总的时间复杂度是线性的。
空间复杂度:O(1),迭代的过程只会产生几个指针,所以它所需要的空间是常数级别的。

思路:
1.判断如果l1.val < l2.val, l1.next = mergeTwoLists(l1.next, l2);
2.否则 l2.next = mergeTwoLists(l2.next, l1);

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

复杂度分析:
时间复杂度:O(n)
空间复杂度:O(1)

public static void main(String[] args) {
        int[] arr = new int[] {1, 2};
        int[] arr2 = new int[] {1, 3};
        ListNode listNode1 = new ListNode(arr);
        System.out.println(listNode1);
        ListNode listNode2 = new ListNode(arr2);
        System.out.println(listNode2);
        System.out.println("合并两个有序的链表" + mergeTwoLists2(listNode1, listNode2));
    }
1->2->NULL
1->3->NULL
合并两个有序的链表1->1->2->3->NULL

上一篇下一篇

猜你喜欢

热点阅读