当前位置:网站首页>剑指 Offer 25. 合并两个排序的链表
剑指 Offer 25. 合并两个排序的链表
2022-07-26 11:05:00 【孙中明】
/** * 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) {
//设置两个指针,分别指向l1和l2,然后对齐进行遍历
//然后插入res 返回res
// 归并
ListNode res = new ListNode(-1);
ListNode pre=res;
while(l1!=null && l2!=null){
if(l1.val<=l2.val){
pre.next=l1;
pre=pre.next;
l1=l1.next;
}else{
pre.next=l2;
pre=pre.next;
l2=l2.next;
}
}
if(l1!=null){
pre.next=l1;
}
if(l2!=null){
pre.next=l2;
}
return res.next;
}
}

边栏推荐
- Pyqt5 rapid development and practice 3.1 QT designer quick start
- Mysql database advanced
- Why give up NPM and turn to yarn
- 配置文件以rc结尾什么意思
- SparseArray of the source code for novices
- Novice source code hashtable
- Bash shell learning notes (4)
- The company cannot access station B
- Access rights - private, public, protected
- MySQL deadlock analysis
猜你喜欢

leetcode-209. 长度最小的子数组(二分、前缀和、滑动窗口)

Classified by the number of 1 in binary number

ArrayList of novice source code

Bash shell learning notes (II)
![[vscode] how to connect to the server remotely](/img/b4/9a80ad995bd589596d8b064215b55a.png)
[vscode] how to connect to the server remotely

复现php一句话木马

The company cannot access station B

LinkedList of source code

3dunity game project practice - first person shooting game

MySQL数据库的简单使用
随机推荐
36. [difference between const function and function]
The company cannot access station B
easyui03
MySQL事务详解
一步一步入门使用g2o解决ICP问题-估计有匹配关系的两组3维点集之间的变换关系
Pre post pytest method
Basic use of logging
Scrapy ip代理无响应
easyui01
Pyqt5 rapid development and practice Chapter 1 understanding pyqt5
二分模板总结
[reprint] the multivariate normal distribution
[idea]如何新建一个项目
Why give up NPM and turn to yarn
配置文件以rc结尾什么意思
Caused by: scala.MatchError: None (of class scala.None$)
线程之间的几种通信方式
MySQL learning notes
Visual conversion of nmap vulnerability scanning results
Bash shell learning notes (VII)