当前位置:网站首页>Sword finger offer | merge two sorted linked lists
Sword finger offer | merge two sorted linked lists
2022-04-22 05:21:00 【Little Yapi】
Java Language . Iterative method , It's a little different from the idea in the book , Not the simplest , For reference only .
1. describe :
Enter two monotonically increasing linked lists , Output the synthesized list of two linked lists , Of course, we need the synthesized list to satisfy the monotone rule .
2. example :
Input : {1,3,5},{2,4,6}
Return value :{1,2,3,4,5,6}
3. Ideas
- Based on the first linked list , Link list 2 Insert into the first linked list .( Will be less than res1.val Insert the value of before )
- Circular linked list 2, Insert each element into the linked list 1 The right place .
4. Code
public class Solution {
public ListNode Merge(ListNode list1,ListNode list2) {
ListNode res = list1;
ListNode res2 = list2;
while (res2 != null) // res2.next == null At the end of the line
{
if (res2.val < res.val)// If less than, insert directly
{
int cur = res.val;
ListNode next = res.next;
ListNode curres2 = new ListNode(res2.val);
// Set the temporary node curres2 Insert into res after
res.next = curres2;
curres2.next = next;
// Switching nodes curres2 and res Of val
res.val = curres2.val;
curres2.val = cur;
res = res.next;
res2 = res2.next;
}
else // Greater than, move the linked list 1 The pointer .
{
if (res.next != null) //res Not reaching the end of the team
{
res = res.next;
}else // Reach the end of the team , Directly link the remaining elements to the linked list 1 Back
{
res.next = res2;
break;
}
}
}
return list1;
}
}
to update : Method 2
Define a temporary linked list , Always attach small nodes to temporary nodes
public ListNode Merge(ListNode list1, ListNode list2) {
ListNode phead = new ListNode(-1);
ListNode cur = phead;
ListNode p1 = list1,p2 = list2;
while(p1!=null && p2!=null){
if(p1.val<p2.val){
cur.next = new ListNode(p1.val);
p1 = p1.next;
cur = cur.next;
}else{
cur.next = new ListNode(p2.val);
p2 = p2.next;
cur = cur.next;
}
}
if(p1!=null){
cur.next = p1;
}
if(p2!=null){
cur.next = p2;
}
return phead.next;
版权声明
本文为[Little Yapi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210621393716.html
边栏推荐
- Event system for ugui source code analysis in unity (9) - input module (2)
- Tree array
- Take yourself to learn paddle (4)
- [redis notes] data structure and object: Dictionary
- Parsing of thread safe classes: (2.8-3.6)
- Learning C language diary from scratch -- day27 minesweeping
- 【高通SDM660平台】(8) --- Camera MetaData介绍
- What are the challenges of Internet of things testing, and how do software testing institutions ensure quality
- [network protocol] why learn network protocol
- Intermediary model (3.28-4.3)
猜你喜欢

Meetup 02 review: Q & A highlights

MySQL数据库第十一次

Generic types in classes and generic types
![[C] file operation](/img/fd/ddf94b0ffa743f2288f723a263a045.png)
[C] file operation

Security challenges in an increasingly tangled web

The signature of the update package is inconsistent with that of the installed app

The eleventh job of MySQL database - Application of view

Temperature control via mqc582tt + PNET

Studio3t expired activation method / and scripts that reset the use date are not available solution / Studio 3T unlimited activation

Four startup modes of activity
随机推荐
The code example of writing data into disk file is as follows:
Codeforces Round #783 (Div. 2) ABCD
Von Neumann architecture
What kind of programming language is this to insert into the database
abcabc
Database (II) addition, deletion, modification and query of MySQL table (basic)
防抖函数和节流函数
Jackson
[C] file operation
Layer 3 switch [vlanif details] enable OSPF and router interworking [ENSP implementation]
Basic configuration of NP and OSPF
One way to disable Google cross domain
Nodejs link redis
Temperature control via mqc582tt + PNET
Distributed transaction Seata
Time complexity and space complexity
Take yourself to learn paddle (4)
Defining "disinformation"
Summary of database Deadlock: (3.7-3.13)
Pyqt5 + yolov5 + MSS to realize a real-time desktop detection software