首页 > 其他分享 >LeetCode-21 Merge Two Sorted Lists

LeetCode-21 Merge Two Sorted Lists

时间:2024-03-27 22:44:07浏览次数:17  
标签:21 list list1 Two Lists list2 current next merged

21. Merge Two Sorted Lists  Easy

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.

Solutions:

class Solution(object):
    # This function merges two sorted linked lists into a single sorted linked list.
    # It takes two non-reduced sequences 'list1' and 'list2' as input.
    def mergeTwoLists(self, list1, list2):
        # Initialize a new ListNode as the head of the merged list
        head = ListNode()
        # Initialize a pointer 'current' to traverse the merged list
        current = head
        
        # Continue until either list1 or list2 becomes None
        while list1 and list2:
            # If the value of the current node in list1 is smaller than list2
            if list1.val < list2.val:
                # Append the current node of list1 to the merged list
                current.next = list1
                # Move to the next node in list1
                list1 = list1.next
            else:
                # Append the current node of list2 to the merged list
                current.next = list2
                # Move to the next node in list2
                list2 = list2.next
            # Move the 'current' pointer to the last appended node
            current = current.next
        
        # Connect the remaining nodes of list1 or list2 to the merged list
        current.next = list1 or list2
        
        # Return the head of the merged list (excluding the dummy node)
        return head.next

 

标签:21,list,list1,Two,Lists,list2,current,next,merged
From: https://www.cnblogs.com/millionyh/p/18100495

相关文章

  • 【办公类-21-11】 三级育婴师 多个二级文件夹的docx合并成docx有页码,转PDF
    背景展示:有页码的操作题背景需求:实操课终于全部结束了,把考试内容(docx)都写好了【办公类-21-10】三级育婴师视频转文字docx(等线小五单倍行距),批量改成“宋体小四、1.5倍行距、蓝色字体、去掉五分钟”-CSDN博客文章浏览阅读787次,点赞9次,收藏7次。【办公类-21-10】三级育婴师......
  • P7137 [THUPC2021 初赛] 切切糕 题解
    题目传送门前置知识博弈论解法由于本题是CF1628D1GameonSum(EasyVersion)的扩展,故先从CF1628D1GameonSum(EasyVersion)讲解。CF1628D1GameonSum(EasyVersion)设\(x_{i}\)表示第\(i\)轮时Alice选择的数。设\(f_{i,j}\)表示已经进行了\(i\)轮,且......
  • ASAA821-EARB0-7H 金手指连接器 SMD卧贴 间距0.5MM 260P DDR4 FOXCONN(富士康)
    ASAA821-EARB0-7H衔接器主要用于电脑和其他电子产品中,完成电气衔接和信号传输。在实践运用中,它可能需要与相应的插座或其他衔接器配合运用。ASAA821-EARB0-7H是富士康(FOXCONN)企业集团出产的一款金手指连接器。以下是关于该产品的部分信息:品牌:FOXCONN/富士康型号:ASAA821-EAR......
  • LeetCodeHot100 链表 160. 相交链表 206. 反转链表 234. 回文链表 141. 环形链表
    160.相交链表https://leetcode.cn/problems/intersection-of-two-linked-lists/description/?envType=study-plan-v2&envId=top-100-likedpublicListNodegetIntersectionNode(ListNodeheadA,ListNodeheadB){intlenA=0;intlenB=0;L......
  • CF1213D1的题解
    (一)直接暴力!!!对于每一个数,枚举它能生成的数。然后对于每一个可能的答案,开长度为\(k\)的优先队列维护,同时统计操作次数和。时间复杂度为\(Θ(\log^2_2n)\)。惊讶地发现顺便把双倍经验给切了。(卡过)(二)AC代码。#include<bits/stdc++.h>usingnamespacestd;intcnt,n,k,su......
  • YC262B [ 20240321 CQYC省选模拟赛 T2 ] 倒水(water)
    题意一面墙上有\(n\)个平台,每个平台是一条连接\((h_i,l_i)\)与\((h_i,r_i)\)的线段。其中\(l_i,r_i\)组成一个\([1,2n]\)的排列。你需要按照某种顺序淹没这些平台,每淹没一个平台,水会顺着线段的两个端点垂直下落。假设每次淹没的水是无限的,若当前的平台没有水,则......
  • test2024.3.21
    多边形题意:有一个长度为\(n\)的\(0/1\)序列,有\(m\)次操作\(u_{i},v_{i}\),若\(a_{u_{i}}=1,a_{v_{i}}=0\)则交换。询问对于\(1,2,\dots,n\)中的每个\(k\),有多少种初始状态,满足恰好有\(k\)个\(1\),并且经过\(m\)次操作后,所有\(1\)形成了一个区间。答案对\(2\)......
  • COMP9021编程原理
    COMP9021编程原理2024年第1学期课业1价值13马克,第7周星期一上午10点到期1.一般事项1.1目标本任务的目的是:•培养解决问题的技能。•以中型Python程序的形式设计和实现问题的解决方案。•练习算术计算、测试、重复、列表和字符串的使用。•使用程序编程。1.2标记该课业价......
  • S-073N 3BHB009884R5211 高压电子元件 用于控制和调整信号的相位
    S-073N3BHB009884R5211高压电子元件是一款专为高压场合设计,用于控制和调整信号相位的电子元件。它集成了多种先进功能,能够满足复杂和精细的相位控制需求。在高压环境中,该电子元件能够实现精确的高压电机控制,从而提高系统的稳定性和效率。它内置的过载保护、短路保护、欠压保......
  • 论文解读(ACDNE)《Adversarial Deep Network Embedding for Cross-Network Node Classif
    Note:[wechat:Y466551|可加勿骚扰,付费咨询]论文信息论文标题:AdversarialDeepNetworkEmbeddingforCross-NetworkNodeClassification论文作者:XiaoShen、QuanyuDai、Fu-laiChung、WeiLu、Kup-SzeChoi论文来源:2020 AAAI论文地址:download 论文代码:download视屏讲解:c......