当前位置:网站首页>[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
边栏推荐
- Ubuntu 20 installing centernet
- Pyuninstaller package exe cannot find the source code when running, function error oserror: could not get source code
- Norm normalization in tensorflow and pytorch of records
- CUDA, NVIDIA driver, cudnn download address and version correspondence
- [※ leetcode refers to offer 32 - II. Print binary tree II from top to bottom (simple)]
- C# 知识
- C knowledge
- Lunch on the 23rd day at home
- Presto on spark supports 3.1.3 records
- [SDU chart team - core] enumeration of SVG attribute class design
猜你喜欢
Minecraft 1.12.2 module development (43) custom shield
Deep analysis of C language pointer (Part I)
41. 缺失的第一个正数
Write table of MySQL Foundation (create table)
DeNO 1.13.2 release
Question brushing plan - depth first search DFS (I)
opencv应用——以图拼图
thinkphp5+数据大屏展示效果
MySQL basic collection
3-5通过XSS获取cookie以及XSS后台管理系统的使用
随机推荐
Tencent cloud has two sides in an hour, which is almost as terrible as one side..
[leetcode refers to offer 32 - III. print binary tree III from top to bottom (medium)]
使用mbean 自动执行heap dump
MySQL advanced common functions
Zhongchuang storage | how to choose a useful distributed storage cloud disk
thinkphp5+数据大屏展示效果
Some thoughts on super in pytorch, combined with code
Keywords static, extern + global and local variables
Some grounded words
How to make Jenkins job run automatically after startup
setInterval、setTimeout、requestAnimationFrame
1. Finishing huazi Mianjing -- 1
Explore ASP Net core read request The correct way of body
Pytorch preserves different forms of pre training models
IOT 设计与开发
Singleton mode
The computer is out of power. How did I pass the terrible interview of Tencent cloud?
Opencv application -- jigsaw puzzle
Recommended usage scenarios and production tools for common 60 types of charts
常用60类图表使用场景、制作工具推荐