zoukankan      html  css  js  c++  java
  • [Leetcode 21] 21 Merge Two Sorted Lists

    Problem:

    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.

    Analysis:

    like merge sort.

    Time complexity is O(m+n), Space complexity is O(m+n)

    Code:

     1 /**
     2  * Definition for singly-linked list.
     3  * public class ListNode {
     4  *     int val;
     5  *     ListNode next;
     6  *     ListNode(int x) {
     7  *         val = x;
     8  *         next = null;
     9  *     }
    10  * }
    11  */
    12 public class Solution {
    13     public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
    14         // Start typing your Java solution below
    15         // DO NOT write main() function
    16         if (l1 == null) return l2;
    17         if (l2 == null) return l1;
    18         
    19         ListNode head = null, tail=null;
    20         while (l1 != null && l2 != null) {
    21             if (l1.val < l2.val) {
    22                 if (head == null) {
    23                     head = l1;
    24                 } else {
    25                     tail.next = l1;
    26                 }
    27                 
    28                 tail = l1;
    29                 l1 = l1.next;
    30             } else {
    31                 if (head == null) {
    32                     head = l2;
    33                 } else {
    34                     tail.next = l2;
    35                 }
    36                 
    37                 tail = l2;
    38                 l2 = l2.next;
    39             }
    40         }
    41         
    42         while (l1 != null) {
    43             tail.next = l1;
    44             tail = l1;
    45             l1 = l1.next;
    46         }
    47         
    48         while (l2 != null) {
    49             tail.next = l2;
    50             tail = l2;
    51             l2 = l2.next;
    52         }
    53         
    54         return head;
    55     }
    56 }
    View Code

    A highly optimized version:

     1 /**
     2  * Definition for singly-linked list.
     3  * public class ListNode {
     4  *     int val;
     5  *     ListNode next;
     6  *     ListNode(int x) {
     7  *         val = x;
     8  *         next = null;
     9  *     }
    10  * }
    11  */
    12 public class Solution {
    13     public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
    14         // Start typing your Java solution below
    15         // DO NOT write main() function
    16         
    17         ListNode head = new ListNode(0), tail=head;
    18         while (l1 != null && l2 != null) {
    19             if (l1.val < l2.val) {     
    20                 tail.next = l1;
    21                 l1 = l1.next;
    22             } else {
    23                 tail.next = l2;
    24                 l2 = l2.next;
    25             }
    26             
    27             tail = tail.next;
    28         }
    29         
    30        if (l1 != null) tail.next = l1;
    31        if (l2 != null) tail.next = l2;
    32         
    33         return head.next;
    34     }
    35 }
    View Code

    Attention:

  • 相关阅读:
    练习44-继承和组合
    CF 1329B Dreamoon Likes Sequences
    CF 1362C Johnny and Another Rating Drop
    CF 1391D 505
    CF 1383B GameGame
    CF1360F Spy-string(暴力)
    Java 之 关键字与标识符
    Java 之 Java 注释与 API
    Java 之 第一个Java程序
    Java 之 Java开发环境
  • 原文地址:https://www.cnblogs.com/freeneng/p/3086480.html
Copyright © 2011-2022 走看看