当前位置:网站首页>[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
边栏推荐
- What about laptop Caton? Teach you to reinstall the system with one click to "revive" the computer
- Presto on spark supports 3.1.3 records
- Graph traversal - BFS, DFS
- Question brushing plan - depth first search DFS (I)
- MySQL基础之写表(创建表)
- 中创存储|想要一个好用的分布式存储云盘,到底该怎么选
- Selenium 显示等待WebDriverWait
- [leetcode refers to offer 32 - III. print binary tree III from top to bottom (medium)]
- 软件测试要怎么学?自学还是培训看完这篇文章你就懂了
- What if Jenkins forgot his password
猜你喜欢

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

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

MySQL进阶之数据的增删改查(DML)

Two Stage Detection

MySQL basic collection

Deep analysis of C language function

thinkphp5+数据大屏展示效果

Express ③ (use express to write interface and cross domain related issues)

Minecraft 1.12.2 module development (43) custom shield

2.整理华子面经--2
随机推荐
PHP的Laravel与Composer部署项目时常见问题
MySQL进阶之表的增删改查
[SDU chart team - core] enumeration of SVG attribute class design
Send email to laravel
[※ leetcode refers to offer 32 - II. Print binary tree II from top to bottom (simple)]
Preliminary analysis of Airbase
What if Jenkins forgot his password
Thinking after learning to type
【SDU Chart Team - Core】SVG属性类设计之枚举
Realrange, reduce, repeat and einops in einops package layers. Rearrange and reduce in torch. Processing methods of high-dimensional data
Tensorflow and pytorch middle note feature map size adjustment to achieve up sampling
C knowledge
深入探究ASP.NET Core读取Request.Body的正确方式
使用mbean 自动执行heap dump
Norm normalization in tensorflow and pytorch of records
100天拿下11K,转岗测试的超全学习指南
The more you use the computer, the slower it will be? Recovery method of file accidental deletion
Minecraft 1.12.2 module development (43) custom shield
又一款数据分析神器:Polars 真的很强大
Ubuntu 20 installing centernet