当前位置:网站首页>[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
边栏推荐
- Google tries to use rust in Chrome
- Google 尝试在 Chrome 中使用 Rust
- Express③(使用Express编写接口、跨域有关问题)
- Write table of MySQL Foundation (create table)
- 41. The first missing positive number
- MySQL基础合集
- Is rust more suitable for less experienced programmers?
- Thinkphp5 + data large screen display effect
- Question brushing plan - depth first search DFS (I)
- Yolov5 NMS source code understanding
猜你喜欢
Linux中,MySQL的常用命令
Question brushing plan - depth first search (II)
Google tries to use rust in Chrome
Chrome 94 introduces the controversial idle detection API, which apple and Mozilla oppose
Chrome 94 引入具有争议的 Idle Detection API,苹果和Mozilla反对
On the three paradigms of database design
Question brushing plan - depth first search DFS (I)
ROS learning notes - tutorial on the use of ROS
The more you use the computer, the slower it will be? Recovery method of file accidental deletion
一些接地气的话儿
随机推荐
Assertionerror: invalid device ID and runtimeerror: CUDA error: invalid device ordinal
Common problems in deploying projects with laravel and composer for PHP
Realrange, reduce, repeat and einops in einops package layers. Rearrange and reduce in torch. Processing methods of high-dimensional data
Thinkphp5 + data large screen display effect
MySQL进阶之表的增删改查
Lunch on the 23rd day at home
C, print the source program of beautiful bell triangle
使用mbean 自动执行heap dump
Fastdfs mind map
laravel 发送邮件
学会打字后的思考
Addition, deletion, modification and query of advanced MySQL data (DML)
Xiaomi mobile phone has abandoned the "Mi" brand all over the world and switched to the full name brand of "Xiaomi"
深入探究ASP.NET Core读取Request.Body的正确方式
Chrome 94 introduces the controversial idle detection API, which apple and Mozilla oppose
Pytorch preserves different forms of pre training models
Valueerror: invalid literal for int() with base 10 conversion error related to data type
Preliminary analysis of Airbase
Ubutnu20 installer centernet