当前位置:网站首页>[leetcode refers to offer 25. Merge two sorted linked lists (simple)]
[leetcode refers to offer 25. Merge two sorted linked lists (simple)]
2022-04-23 21:21:00 【Minaldo7】
subject :
Enter two ascending ordered linked lists , Merge these two linked lists and make the nodes in the new linked list still be sorted incrementally .
Example 1:
Input :1->2->4, 1->3->4
Output :1->1->2->3->4->4
Limit :
0 <= Chain length <= 1000
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/he-bing-liang-ge-pai-xu-de-lian-biao-lcof
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
The problem solving process :
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if(l1 == null && l2 != null)
return l2;
if(l1 != null && l2 == null)
return l1;
if(l1 == null || l2 == null)
return null;
ListNode pre = new ListNode(-1);
ListNode head = pre;
while(l1 != null && l2 != null){
if(l1.val > l2.val){
pre.next = l2;
pre = pre.next;
l2 = l2.next;
}else{
pre.next = l1;
pre = pre.next;
l1 = l1.next;
}
}
pre.next= l1==null ? l2:l1;
return head.next;
}
}
Execution results :

版权声明
本文为[Minaldo7]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/111/202204210544479170.html
边栏推荐
- 韩国或将禁止苹果和谷歌向开发者抽佣 创全球首例
- Norm normalization in tensorflow and pytorch of records
- C, print the source program of beautiful bell triangle
- MySQL基础之写表(创建表)
- Graph traversal - BFS, DFS
- Flomo software recommendation
- Deep analysis of C language function
- Addition, deletion, modification and query of MySQL advanced table
- Zhongchuang storage | how to choose a useful distributed storage cloud disk
- IOT 设计与开发
猜你喜欢

Sharpness difference (SD) calculation method of image reconstruction and generation domain index

Question brushing plan - depth first search (II)

中创存储|想要一个好用的分布式存储云盘,到底该怎么选

GSI-ECM工程建设管理数字化平台

笔记本电脑卡顿怎么办?教你一键重装系统让电脑“复活”

Common commands of MySQL in Linux

What if Jenkins forgot his password

C#,打印漂亮的贝尔三角形(Bell Triangle)的源程序

MySQL进阶之表的增删改查

Win 11K in 100 days, super complete learning guide for job transfer test
随机推荐
Sharpness difference (SD) calculation method of image reconstruction and generation domain index
How to learn software testing? Self study or training? After reading this article, you will understand
1.整理华子面经--1
Chrome 94 引入具有争议的 Idle Detection API,苹果和Mozilla反对
启牛学堂有用吗,推荐的证券账户是否安全
Win 11K in 100 days, super complete learning guide for job transfer test
软件测试要怎么学?自学还是培训看完这篇文章你就懂了
韩国或将禁止苹果和谷歌向开发者抽佣 创全球首例
如何发挥测试策略的指导性作用
Selenium displays webdriverwait
Assertionerror: invalid device ID and runtimeerror: CUDA error: invalid device ordinal
Centralized record of experimental problems
YOLOv5 Unable to find a valid cuDNN algorithm to run convolution
Detailed explanation of basic assembly instructions of x86 architecture
pytorch 1.7. The model saved by X training cannot be loaded in version 1.4 or earlier
亚马逊和Epic将入驻,微软应用商城向第三方开放
Express ③ (use express to write interface and cross domain related issues)
Solve importerror: cannot import name 'imread' from 'SciPy misc‘
[leetcode refers to offer 27. Image of binary tree (simple)]