当前位置:网站首页>【LeetCode】21. 合并两个有序链表 - Go 语言题解
【LeetCode】21. 合并两个有序链表 - Go 语言题解
2022-06-28 23:42:00 【想变厉害的大白菜】
一、题目描述
将两个升序链表合并为一个新的升序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
示例 1:

输入:l1 = [1,2,4], l2 = [1,3,4]
输出:[1,1,2,3,4,4]
示例 2:
输入:l1 = [], l2 = []
输出:[]
示例 3:
输入:l1 = [], l2 = [0]
输出:[0]
提示:
两个链表的节点数目范围是 [0, 50]
-100 <= Node.val <= 100
l1 和 l2 均按 非递减顺序 排列
原题链接:
https://leetcode.cn/problems/merge-two-sorted-lists/
二、我的题解
解题思路
- 创建第三个新的链表 head ,它的 head 首先指向头比较小的那个链表,然后不断更新新链表的 Next 域来完成合并;
- 新链表的尾 tail 初始化为它的 head ;
- 不断比较 list1 和 list2 当前的 Val ,比较小的作为新表 tail 的 Next ;
- 直到 list1 和 list2 的其中一个表结束啦,就把另一个表剩下的部分续在新表的 tail 上就行啦;
- 返回 head 。
Go 语言代码
/** * 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
// 找头
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
}
}
评判结果:

边栏推荐
- 2022 PMP project management examination agile knowledge points (4)
- The picture display on the left of the two column layout is determined by the content height on the right
- TDD and automated testing
- IDC: Alibaba cloud ranks first in the market share of China's data governance platform in 2021
- ctfshow XSS
- 第四章 存储器管理练习
- Finally, someone explained the cloud native architecture
- Three communication skills in software testing
- [conception de la machine d'état] Moore, Mealy State Machine, Three - stage, Two - stage, one - stage State Machine Writing Specification
- At the end of June, how many people in Kangkang are ready to change jobs
猜你喜欢

华为22级专家十年心血终成云原生服务网格进阶实战文档,是真的6

Learn binary tree like this

stm32F407-------LCD

VSCode里使用条件断点(基于GDB)

At the end of June, how many people in Kangkang are ready to change jobs

机器学习4-降维技术

PHP 使用endroid/qrcode 二维码生成, GD库生成分享海报

Machine learning 6-decision tree

The secondary market is full of bad news. How should the market go next? One article will show you the general trend

Lock4j -- distributed locking Middleware -- use / instance
随机推荐
Class extension and optional type extension of dart
PHP利用CURL实现登录网站后下载Excel文件
fio的IO重放功能
What are some tips to improve your interview success rate?
[stm32 Hal library] serial port communication
"Five considerations" for safe use of the Internet
What will be done after digital IC Verification?
Baidu knows the crawler, and obtains the dialogue below the comment according to the question Id, clue ID and comment ID
Picture 64base transcoding and decoding
What are the virtual machine software? What are their respective roles?
That's how he did it!
Stm32f407 ------- GPIO input experiment
请问指南针股票软件可靠吗?在上面交易股票安全吗?
Counting sorting and stability of sorting
Rongyun communication solution solves the pain points of enterprise communication
stm32F407-------通用定时器
Matlab learning notes (6) upsample function and downsample function of MATLAB
Design e-commerce seckill system
Do you know all the wonderful functions of the vlookup function?
frameworks/base/core/res/res/values/symbols.xml:3915: error: no definition for declared symbol解决办法