# LeetCode 21：Merge Two Sorted Lists

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

Example:

Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4

Java源码

`package com.maoxiaomeng;import java.util.ArrayList;import java.util.Collections;import java.util.List;/** * @author lihui * @date 2018/3/26 12:31 */class ListNode {    int val;    ListNode next;    ListNode(int x) {        val = x;    }}public class Solution {    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {        List<Integer> myList = new ArrayList<Integer>();        while (l1 != null) {            myList.add(l1.val);            l1 = l1.next;        }        while (l2 != null) {            myList.add(l2.val);            l2 = l2.next;        }        Collections.sort(myList);        System.out.println(myList);                ListNode current = new ListNode(myList.get(0));        ListNode head = current;        for (int i = 1; i < myList.size(); i++) {            current.next = new ListNode(myList.get(i));            current.next = current.next.next;        }        return head.next;    }}`

`package com.maoxiaomeng;/** * @author lihui * @date 2018/3/26 12:31 */class ListNode {    int val;    ListNode next;    ListNode(int x) {        val = x;    }}public class Solution {    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {        if (l1 == null) {            return l2;        }        if (l2 == null) {            return l1;        }        ListNode current = new ListNode(0);        ListNode head = current;        /**         * l1: 0->2->4->6         * l2: 1->3->5->7         * l : 0->1->2->3->4->5->6->7         */        while (l1 != null && l2 != null) {            if (l1.val <= l2.val) {                current.next = l1;                l1 = l1.next;            } else {                current.next = l2;                l2 = l2.next;            }            current = current.next;        }        if (l1 != null) {            current.next = l1;        }        if (l2 != null) {            current.next = l2;        }        return head.next;    }}`

`package com.maoxiaomeng;/** * @author lihui * @date 2018/3/26 12:31 */class ListNode {    int val;    ListNode next;    ListNode(int x) {        val = x;    }}public class Solution {    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {        if (l1 == null) {            return l2;        }        if (l2 == null) {            return l1;        }        /**         * l1: 0->2->4->6         * l2: 1->3->5->7         * l : 0->1->2->3->4->5->6->7         */        if (l1.val <= l2.val) {            ListNode head = l1;            head.next = mergeTwoLists(l1.next, l2);            return head;        } else {            ListNode head = l2;            head.next = mergeTwoLists(l1, l2.next);            return head;        }    }}`

`package com.maoxiaomeng;/** * @author lihui * @date 2018/3/26 12:31 */class ListNode {    int val;    ListNode next;    ListNode(int x) {        val = x;    }}public class Solution {    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {        if (l1 == null) {            return l2;        }        if (l2 == null) {            return l1;        }        /**         * l1: 0->2->4->6         * l2: 1->3->5->7         * l : 0->1->2->3->4->5->6->7         */        if (l1.val <= l2.val) {            l1.next = mergeTwoLists(l1.next, l2);            return l1;        } else {            l2.next = mergeTwoLists(l1, l2.next);            return l2;        }    }}`