LeetCode //C - 21. Merge Two Sorted Lists

21. Merge Two Sorted Lists

You are given the heads of two sorted linked lists list1 and list2.

Merge the two lists into one sorted list. The list should be made by splicing together the nodes of the first two lists.

Return the head of the merged linked list.
 

Example 1:

在这里插入图片描述

Input: list1 = [1,2,4], list2 = [1,3,4]
Output: [1,1,2,3,4,4]

Example 2:

Input: list1 = [], list2 = []
Output: []

Example 3:

Input: list1 = [], list2 = [0]
Output: [0]

Constraints:
  • The number of nodes in both lists is in the range [0, 50].
  • -100 <= Node.val <= 100
  • Both list1 and list2 are sorted in non-decreasing order.

From: LeetCode
Link: 21. Merge Two Sorted Lists


Solution:

Ideas:
  • We start by creating a dummy node that will serve as a placeholder to the start of our new list.
  • The tail pointer is used to keep track of the end of the merged list.
  • We loop through both list1 and list2 until we reach the end of one of them.
  • In each iteration, we compare the values of the current nodes and append the smaller one to the merged list, moving the tail and the appropriate list pointer (list1 or list2) forward.
  • If one of the lists is exhausted before the other, we simply append the remainder of the non-exhausted list to the merged list.
  • Finally, we return the next node after dummy as it points to the start of our merged list.
Code:
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) {
   
    // Create a dummy node to act as the start of the merged list.
    struct ListNode dummy;
    // Use a tail node to keep track of the last node in the merged list.
    struct ListNode *tail = &dummy;
    // The dummy node initially points to NULL.
    dummy.next = NULL;

    while (list1 && list2) {
   
        if (list1->val < list2->val) {
   
            // If the current node in list1 is less, add it to the merged list.
            tail->next = list1;
            list1 = list1->next;
        } else {
   
            // If the current node in list2 is less or equal, add it to the merged list.
            tail->next = list2;
            list2 = list2->next;
        }
        // Move the tail pointer forward.
        tail = tail->next;
    }

    // If there are remaining nodes in either list, append them to the merged list.
    if (list1) {
   
        tail->next = list1;
    } else {
   
        tail->next = list2;
    }

    // The merged list is after the dummy node.
    return dummy.next;
}

相关推荐

  1. MergeTwoSortedLists 【合并有序链表】

    2023-12-11 09:30:06       65 阅读
  2. LeetCode 21

    2023-12-11 09:30:06       43 阅读
  3. Day.21

    2023-12-11 09:30:06       40 阅读
  4. 面试经典150题(21-26)

    2023-12-11 09:30:06       64 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-11 09:30:06       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-11 09:30:06       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-11 09:30:06       82 阅读
  4. Python语言-面向对象

    2023-12-11 09:30:06       91 阅读

热门阅读

  1. 什么是css初始化

    2023-12-11 09:30:06       56 阅读
  2. leetcode每日一题38

    2023-12-11 09:30:06       58 阅读
  3. 在Vue 3中如何禁止网页返回到上一页

    2023-12-11 09:30:06       53 阅读
  4. Python基础期末复习 新手

    2023-12-11 09:30:06       56 阅读
  5. 程序员常用英文单词

    2023-12-11 09:30:06       34 阅读
  6. android-xml语法

    2023-12-11 09:30:06       57 阅读
  7. MapReduce

    2023-12-11 09:30:06       38 阅读
  8. 华为鸿蒙HarmonyOS应用开发者高级认证试题及答案

    2023-12-11 09:30:06       164 阅读
  9. web项目创建流程框架

    2023-12-11 09:30:06       58 阅读
  10. 《C++新经典设计模式》之第15章 适配器模式

    2023-12-11 09:30:06       52 阅读
  11. C++(14):获取类型在tuple中的索引

    2023-12-11 09:30:06       50 阅读