当前位置:网站首页>每日一题-合并两个有序链表-0720
每日一题-合并两个有序链表-0720
2022-08-05 05:17:00 【菜鸡程序媛】
题目
将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
思路
- 当其中一个链表为空的时候,直接返回另一个链表
- 比较两个链表当前节点的值,小的那个当作链表头节点,next指针指向下一个smaller节点,递归结束后,返回链表头节点
代码
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
if(list1 == null){
return list2;
}
if(list2 == null){
return list1;
}
if(list1.val < list2.val){
list1.next = mergeTwoLists(list1.next, list2);
return list1;
}else{
list2.next = mergeTwoLists(list1, list2.next);
return list2;
}
}
边栏推荐
猜你喜欢
随机推荐
CVPR 2022 |节省70%的显存,训练速度提高2倍
八、响应处理——ReturnValueHandler匹配返回值处理器并处理返回值原理解析
LeetCode刷题之第74题
6k+ star,面向小白的深度学习代码库!一行代码实现所有Attention机制!
[Pytorch study notes] 9. How to evaluate the classification results of the classifier - using confusion matrix, F1-score, ROC curve, PR curve, etc. (taking Softmax binary classification as an example)
LeetCode刷题之第746题
Comparison and summary of Tensorflow2 and Pytorch in terms of basic operations of tensor Tensor
UiPath简介
沁恒MCU从EVT中提取文件建立MounRiver独立工程
关于存储IOPS你必须了解的概念
七、请求处理——Map、Model类型参数处理原理
(C语言)strlen、strcpy、strcat、strcmp、strstr函数的模拟实现
CVPR2021 - Inception Convolution with Efficient Dilation Search
伪RTOS-ProroThread在CH573芯片上的移植
Detailed explanation of BroadCast Receiver (broadcast)
C语言查看大小端(纯代码)
LeetCode刷题之第416题
栈的应用——力扣 20.有效的括号
数控直流电源
MySQL