zoukankan      html  css  js  c++  java
  • 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
     class Solution {
        public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
            ListNode dummy = new ListNode(-1), cur = dummy;
            while (l1 != null && l2 != null) {//始终记住,l1,l2,cur只是refer的name,不是node本身,所以即使变化了,
                                                //只要在之前有一个(比如dummy)抓住他们,node就不会丢失。变得只是refer的位置
                if (l1.val < l2.val) {
                    cur.next = l1;
                    l1 = l1.next;
                } else {
                    cur.next = l2;
                    l2 = l2.next;
                }
                cur = cur.next;
            }
            cur.next = (l1 != null) ? l1 : l2;
            return dummy.next;
        }
    }
  • 相关阅读:
    6.7
    6.5
    6.4随笔
    js 插件
    js插件
    web中集成jdbc
    jsp
    web容器中的servlet
    web服务器的监听器,过滤器
    几款js工具的使用
  • 原文地址:https://www.cnblogs.com/wentiliangkaihua/p/11470834.html
Copyright © 2011-2022 走看看