当前位置:网站首页>[sword finger offer] interview question 25 Merge two ordered linked lists
[sword finger offer] interview question 25 Merge two ordered linked lists
2022-06-13 04:22:00 【LuZhouShiLi】
Interview questions 25. Merge two ordered lists
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 .
Ideas
- Set up dummy Is an empty node , As a new linked list , So the last thing back is dummy.next, Set up cur Is the current node , from dummy Start
- Both linked lists are non empty as a loop condition , Select the smaller nodes in two linked lists at a time , The corresponding linked list node moves back one bit
- After each cycle cur Also move back one bit , If after the cycle ends , The list is not empty , take cur Point to non empty linked list
- Last , return dummy.next
Code
/** * 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) {
ListNode dummy = new ListNode();// Define a dummy node
ListNode cur = dummy;
while(l1 != null && l2 != null)
{
// Select a smaller node value each time
if(l1.val <= l2.val)
{
cur.next = l1;
l1 = l1.next;
}
else
{
cur.next = l2;
l2 = l2.next;
}
cur = cur.next;
}
// If the nodes of a linked list are not merged , Then merge all the nodes in the linked list into the new linked list
if(l1 != null)
{
cur.next = l1;
}
if(l2 != null)
{
cur.next = l2;
}
return dummy.next;
}
}
边栏推荐
- Dumi construit un blog documentaire
- Advanced Mathematics (Seventh Edition) Tongji University exercises 1-2 personal solutions
- Unity Shader 学习 004-Shader 调试 平台差异性 第三方调试工具
- VGA display based on de2-115 platform
- Discussion sur la modélisation de la série 143
- [笔记]vs2015 编写汇编masm32之使用MASM32库
- Application of dagger2 learning module (II)
- 干预分析 + 伪回归
- UE4 learning notes - functions of terrain tool
- Idea Download
猜你喜欢

MCU: NEC protocol infrared remote controller

Tree array explanation

Ladder race

Redis data persistence

高等数学(第七版)同济大学 习题1-3 个人解答

Differences and relations between three-tier architecture and MVC

Dumi builds a document blog

【ZeloEngine】本地化流程/ImGui中文化

Common terms of electromagnetic compatibility

Unity shader learning 004 shader debugging platform difference third-party debugging tools
随机推荐
120. triangle minimum path sum - Dynamic Planning
[test development] advanced part - Classification of various test technologies
EIA map making - data processing + map making
Principle, composition and functions of sensors of Dajiang UAV flight control system
Modeling discussion series 143 data processing, analysis and decision system development
Translation of ego planner papers
Redis hyperloglog cardinality statistics algorithm
Real time requirements for 5g China Unicom repeater network management protocol
MCU: RS485 communication and Modbus Protocol
十億數據量 判斷元素是否存在
[test development] use case
Suffix Automaton
UE4 learning notes - functions of terrain tool
Et framework -22 creating serverinfo entities and events
环评图件制作-数据处理+图件制作
史上最详细的Swin-Transformer 掩码机制(mask of window attentation)————shaoshuai
干预分析 + 伪回归
Common encryption and decryption function encapsulation - AES encryption and decryption
Promise combined with await
On the value of line height