leetcode-21. Merge Two Sorted Lists

2021-03-02 李安替
題目描述(簡單難度)

題目描述:將兩個升序鍊表合併為一個新的 升序 鍊表並返回。新鍊表是通過拼接給定的兩個鍊表的所有節點組成的。

解法一 迭代

遍歷兩個鍊表。

Java
class ListNode{
 int val;
 ListNode next;
 ListNode(int val){
  this.val=val;
 }
}
public class Merge_Two_Sorted_Lists {
 public static ListNode mergertwolists(ListNode l1,ListNode l2) {
  ListNode h=new ListNode(0);
  ListNode ans=h;
  while(l1!=null && l2!=null) {
   if(l1.val<l2.val) {
    h.next=l1;
    h=h.next;
    l1=l1.next;
   }else {
    h.next=l2;
    h=h.next;
    l2=l2.next;
   }
  }
  if(l1==null) h.next=l2;
  if(l2==null) h.next=l1;
  return ans.next;
 }
 public static void main(String args[]) {
  
  ListNode l1=new ListNode(1);
  ListNode p=l1;
  p.next=new ListNode(2);
  p=p.next;
  p.next=new ListNode(4);
  
  ListNode l2=new ListNode(1);
  ListNode q=l2;
  q.next=new ListNode(3);
  q=q.next;
  q.next=new ListNode(4);
  
  ListNode ans=mergertwolists(l1,l2);
  while(ans!=null) {
   System.out.println(ans.val);
   ans=ans.next;
  }
 }
}

時間複雜度:O(m + n)。

空間複雜度:O(1)。

Python
class Solution(object):
    def mergeTwoLists(self, l1, l2):
        l3= ListNode(0)
        h = l3
        while(l1!=None and l2!=None):
            if(l1.val<l2.val):
                h.next=l1
                h=h.next
                l1=l1.next
            else:
                h.next=l2
                h=h.next
                l2=l2.next
        if l1==None:
            h.next=l2
        if l2==None:
            h.next=l1
        return l3.next

在這裡插入圖片描述解法二 遞歸在這裡插入圖片描述Java
class Solution {
    public 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;
        }
    }
}

在這裡插入圖片描述Python
class Solution(object):
    def mergeTwoLists(self, l1, l2):
        if l1==None:
            return l2
        if l2==None:
            return l1
        if l1.val < l2.val:
            l1.next=self.mergeTwoLists(l1.next,l2)
            return l1
        else:
            l2.next=self.mergeTwoLists(l2.next,l1)
            return l2  

在這裡插入圖片描述參考文獻https://www.youtube.com/watch?v=qckKEYP9bBA

相關焦點

  • LeetCode-21.Merge Two Sorted Lists | 合併兩個有序鍊表
    題目LeetCode[1]LeetCode-cn[2]Merge two sorted linked lists and
  • LeetCode Top 100 高頻算法題 21. Merge Two Sorted Lists
    LeetCode算法題有一個技巧:先看每道題的example,大部分情況看完example就能理解題目意思;如果看完example還有疑惑,可以再回過頭來看英文題目,這樣也可以節省一點時間~Merge two sorted linked lists and return it as a
  • LeetCode 23. Merge k Sorted Lists
    [傳送門](https://leetcode.com/problems/merge-k-sorted-lists/)## 題意
  • LeetCode-23.合併K個排序鍊表(Merge k Sorted Lists)
    示例:輸入:[ 1->4->5, 1->3->4, 2->6]輸出: 1->1->2->3->4->4->5->6來源:力扣(LeetCode)連結:https://leetcode-cn.com/problems/merge-k-sorted-lists
  • LeetCode Top 100 高頻算法題 23. Merge k Sorted Lists
    linked-list is sorted in ascending order.Merge all the linked-lists into one sorted linked-list and return it.
  • leetcode-23. Merge k Sorted Lists
    = {l1,l2,l3};  ListNode ans=mergeKLists(lists);  while(ans!# class ListNode(object):#     def __init__(self, val=0, next=None):#         self.val = val#         self.next = nextclass Solution(object):    def mergeKLists(self, lists):
  • 合併兩個有序鍊表 | Leetcode題解
    .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
  • Go 實現 LeetCode 全集
    /problems/two-sum/[X] Best Time to Buy and Sell Stock - https://leetcode.com/problems/best-time-to-buy-and-sell-stock/[X] Contains Duplicate - https://leetcode.com/problems/contains-duplicate
  • leetCode第4題:Median of Two Sorted Arrays
    原題:There are two sorted arrays A and B of size m and n respectively
  • Crazy LeetCode — 4. Median of Two Sorted Arrays
    Median of Two Sorted Arrays[1]題目There are two sorted arrays 「nums1」 and 「nums2」 of size m and n respectively.
  • LeetCode鍊表合併題目解析
    leetcode鍊表合併相關題目:21_merge-two-sorted-lists1699_merge-in-between-linked-lists21_merge-two-sorted-lists1、題目描述將兩個升序鍊表合併為一個新的 升序 鍊表並返回。
  • ​LeetCode刷題實戰23:合併K個升序鍊表
    今天和大家聊的問題叫做合併K個升序鍊表,我們先來看題面:https://leetcode-cn.com/problems/merge-k-sorted-lists/Given an array of linked-lists lists, each linked list is sorted in ascending order.Merge
  • Leetcode刷題-鍊表
    (也就是已排序的最大的那個)遍歷鍊表,每次令當前節點為last_sorted的下一個節點如果當前節點值大於last_sorted的值,那麼直接令last_sorted等於下一個節點否則先讓last_sorted指向當前節點的下一個節點,再將其插入到前面的鍊表中時間複雜度:O(n^2) 空間複雜度:O(1)代碼
  • LeetCode-4.尋找兩個正序數組的中位數(Median of Two Sorted Arrays)
    >示例 1:nums1 = [1, 3]nums2 = [2]則中位數是 2.0示例 2:nums1 = [1, 2]nums2 = [3, 4]則中位數是 (2 + 3)/2 = 2.5來源:力扣(LeetCode)連結:https://leetcode-cn.com