当前位置:网站首页>【LeetCode】21. Merge two ordered linked lists - go language solution
【LeetCode】21. Merge two ordered linked lists - go language solution
2022-06-28 23:48:00 【Cabbage that wants to become powerful】
List of articles
One 、 Title Description
Merge two ascending lists into a new ascending list and return . The new linked list is made up of all the nodes of the given two linked lists .
Example 1:

Input :l1 = [1,2,4], l2 = [1,3,4]
Output :[1,1,2,3,4,4]
Example 2:
Input :l1 = [], l2 = []
Output :[]
Example 3:
Input :l1 = [], l2 = [0]
Output :[0]
Tips :
The range of the number of nodes in the two linked lists is [0, 50]
-100 <= Node.val <= 100
l1 and l2 All according to Non decreasing order array
Original link :
https://leetcode.cn/problems/merge-two-sorted-lists/
Two 、 My explanation
Their thinking
- Create a third new linked list head , its head First, point to the linked list with a small head , And then constantly update the new linked list Next Domain to complete the merge ;
- Tail of new linked list tail Initialize to its head ;
- Keep comparing list1 and list2 Current Val , The smaller ones are used as new tables tail Of Next ;
- until list1 and list2 One of the tables is over , Just add the rest of the other table to the new table tail Just go ;
- return head .
Go The language code
/** * Definition for singly-linked list. * type ListNode struct { * Val int * Next *ListNode * } */
func mergeTwoLists(list1 *ListNode, list2 *ListNode) *ListNode {
if list1 == nil && list2 == nil{
return nil
}
if list1 == nil{
return list2
}
if list2 == nil{
return list1
}
var head,tail *ListNode
// Small change
if list1.Val <= list2.Val{
head = list1
list1 = list1.Next
}else{
head = list2
list2 = list2.Next
}
tail = head
for list1 != nil && list2 != nil{
if list1.Val <= list2.Val{
tail.Next = list1
tail = tail.Next
list1 = list1.Next
}else{
tail.Next = list2
tail = tail.Next
list2 = list2.Next
}
}
if list1 == nil && list2 == nil{
return head
}else if list1 == nil{
tail.Next = list2
return head
}else{
tail.Next = list1
return head
}
}
Judge the result :

边栏推荐
- SQL note 2 [MySQL]
- Mysql-5.7.30-winx64 installation free download and installation tutorial
- Stm32f407 ------ serial (serial port) communication
- Huawei's level 22 experts have worked hard for ten years to complete the advanced practical document of cloud native service grid. 6
- The second session of question swiping and clock out activity -- solving the switching problem with recursion as the background (III)
- mysql-5.7.30-winx64免安装版下载安装教程
- frameworks/base/core/res/res/values/symbols. Xml:3915: error: no definition for declared symbol solution
- stm32F407-------RTC实时时钟
- Stm32f407-------- NVIC interrupt priority management
- Advice to friends
猜你喜欢
![[state machine design] Moore, Mealy state machine, three-stage, two-stage and one-stage state machine writing specification](/img/48/e29f34aff7cc437bfb574591d54e3d.png)
[state machine design] Moore, Mealy state machine, three-stage, two-stage and one-stage state machine writing specification

TypeScript -- 第一节:基础类型

stm32F407-------LCD

stm32F407-------通用定时器

随笔记:插入排序 --from wcc

Blue Bridge Cup top ten common heaven level skill - breath of water The type of one recursion

Stm32f407----- capacitive touch button

Auto encoder

Typescript -- Section 1: basic types

Be on the list again! Know that Chuangyu was selected as one of the top 50 competitive enterprises in China's network security industry in 2022
随机推荐
股票开户在网上开通安全吗?
urllib. Parse parses the parameters in the URL connection
What will be done after digital IC Verification?
一条update语句到底加了多少锁?带你深入理解底层原理
表单校验问题——el-select(初始化页面自动触发校验解决办法)
图片64base转码与解码
Basic operation of MySQL database: import hellodb SQL and query as required; Create account and authorize
urllib.parse 解析url连接中的参数
stm32F407-------跑马灯、蜂鸣器
再次上榜!知道创宇入选2022中国网安产业竞争力50强
stm32F407-------寄存器地址名称映射分析
三個pwn題
Rongyun communication solution solves the pain points of enterprise communication
Baidu knows the crawler, and obtains the dialogue below the comment according to the question Id, clue ID and comment ID
Behaviortree in ros2
百度知道爬虫,根据问题id,线索id,评论id获取评论下面的对话
Yyds dry inventory solution sword finger offer: maximum sum of continuous subarrays (II)
三个pwn题
剑指 Offer 12. 矩阵中的路径
TypeScript -- 第一节:基础类型