当前位置:网站首页>【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
}
}
评判结果:

边栏推荐
- Scrapy uses xlwt to implement the exporter that exports data in Excel format
- Interviewer: what is the internal implementation of strings in redis?
- "Five considerations" for safe use of the Internet
- urllib.parse 解析url连接中的参数
- 股票开户在网上开通安全吗?
- 设计电商秒杀系统
- Online yaml to JSON tool
- Stm32f407 ------- RTC real time clock
- 油猴脚本学习
- frameworks/base/core/res/res/values/symbols.xml:3915: error: no definition for declared symbol解决办法
猜你喜欢

第四章 存储器管理练习

【OpenCV】—线性滤波:方框滤波、均值滤波、高斯滤波

stm32F407-------通用定时器
![[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

Stm32f407 ------ running lamp and buzzer

Online yaml to JSON tool
![[stm32 HAL库] 串口通信](/img/2c/23a2ecf75141b8f38ab99ac6b2eaef.png)
[stm32 HAL库] 串口通信

stm32F407-------LCD

从SQL注入绕过最新安全狗WAF中学习fuzz

The second session of question swiping and clock out activity -- solving the switching problem with recursion as the background (2)
随机推荐
stm32F407-------GPIO输入实验
Mysql-5.7.30-winx64 installation free download and installation tutorial
Machine learning 6-decision tree
关联线探究,如何连接流程图的两个节点
Class extension and optional type extension of dart
Count the number of arrays with pointers
Matlab learning notes (6) upsample function and downsample function of MATLAB
【软件分析】软件分析、设计与建模迭代式详解
融云通信解决方案 破解企业沟通痛点
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
【SSM】报错 Access denied for user ‘WYF‘@‘localhost‘ (using password: YES) 数据的用户名变成了电脑的用户名
Stm32f407-------- NVIC interrupt priority management
[stm32 Hal library] serial port communication
SQL note 2 [MySQL]
[word Tutorial Series Part 1] how to remove arrows in word tables
网上注册股票开户很困难么?在线开户是安全么?
TDD and automated testing
IDC: Alibaba cloud ranks first in the market share of China's data governance platform in 2021
TDD case practice
Rongyun communication solution solves the pain points of enterprise communication