当前位置:网站首页>合并两个有序链表---2022/02/24
合并两个有序链表---2022/02/24
2022-06-11 17:18:00 【城猪猪】
题目描述
将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
题目来源
解题思考
思路:
第一种方法:边比较边放入新的链表(这里存在一个比较边界的问题) 但是这里的时间复杂度为n的平方。方法描述在进行添加之前判断链表是否有效,如果链表1(2)已经指向末尾,把链表2(1)剩下元素全部添加到新建链表中,并直接返回。如果链表1当前元素<链表2当前元素,链表1当前元素添加新链表中,链表1指向下一个元素,否则链表2当前元素添加新链表中,链表2指向下一个元素
第二种方法:分别取出链表1和链表2的数据放到list(补充:这里最好不选择list,因为调用排序接口只针对数组)里面,然后进行排序,排序结束之后,再逐一添加到新建链表中,返回头节点。时间复杂度为n。且操作更为简洁。
知识补充
1、list、set、map的区别。
以及如何创建。
创建list
List <> a=new ArrayList<>();
创建set
Set <> a=new LinkedHashSet<>();
创建map
Map <> a=new HashMap<>();
2、如何在方法外添加节点的方法(就是cur和head目前的联系关系自己还是有点懵)
代码实现
class Solution {
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
int []temp=new int [100];
int j=0;
while(list1!=null)
{
temp[j++]=list1.val;
list1=list1.next;
}
while(list2!=null)
{
temp[j++]=list2.val;
list2=list2.next;
}
int[] temp1=new int[j];
for(int k=0;k<j;k++)
{
temp1[k]=temp[k];
}
if(temp1.length==0)return null;
Arrays.sort(temp1);
ListNode head=new ListNode(temp1[0],null);
int i=1;
ListNode curent=head;
//由于节点类没有定义其后添加节点的方法
//所以在这里一直打圈圈,走不出来。。。。
//而后解决了,但是还是不是很清楚,head和curent如何对上的?
while(i<j)
{
ListNode newNode = new ListNode(temp1[i]);
curent.next = newNode;
curent=curent.next;
i++;
}
return head;
}
}
性能评估

依旧是以内存换取时间。
边栏推荐
- LeetCode-384. Scramble array
- Docker安装mysql5.7(开启binlog功能、修改字符)
- 导出数据提示--secure-file-priv选项问题的解决方法
- From a "trendsetter" to a "wind chaser", can master Kang still lead the market?
- Custom or subscription? What is the future development trend of China's SaaS industry?
- LeetCode-1005. K 次取反后最大化的数组和
- 【线上问题】Timeout waiting for connection from pool 问题排查
- Biden ordered to enforce the zero trust structure
- AXI协议基础知识
- A simple understanding of closures
猜你喜欢

Recyclerview cache reuse analysis, source code interpretation

Bentley uses authing to quickly integrate application system and identity

Vscode configures eslint to automatically format with an error "the setting is deprecated. use editor.codeactionsonsave instead with a source“

Connect the server with springboard / fortress through xshell

搜狐全员遭诈骗,暴露哪些问题?

Vscode configures eslint to automatically format an error "auto fix is enabled by default. use the single string form“

Use of forcescan in SQL server and precautions
![[pytest learning] after the pytest case fails to execute, the others will not be executed](/img/c7/52ae88cde65bdd12ae8b86df5c477f.png)
[pytest learning] after the pytest case fails to execute, the others will not be executed

LeetCode——42. Connected to rainwater (double pointer)

CLP information -5 keywords to see the development trend of the financial industry in 2022
随机推荐
Error: error summary of pointer as function parameter
使用exe4j 将.jar文件打包为.exe文件
LeetCode-859. 亲密字符串
ffmpeg硬编解码 Inter QSV
How to become an optimist organization?
(validation file) validatejarfile report errors
C语言:使用.h和.c文件遇到的问题总结
Talk about the interview questions of the collection
04_ Feature engineering feature selection
DFS and BFS notes (I) breadth first search based on C language
用实际案例分析PMP与ACP应该考哪个?哪个更有用?
From manufacturing to "intelligent manufacturing", explore the way for manufacturing enterprises to break the situation
Association relationship
Tornado environment construction and basic framework construction -- familiar Hello World
Is it safe for Xiaobai to open an account directly on the flush?
spawn ./gradlew EACCES at Process.ChildProcess._handle.onexit
满k叉树编号为 i 的节点的孩子编号公式推导
Dynamic: capturing network dynamics using dynamic graph representation learning
Use exe4j to convert The jar file is packaged as Exe file
AXI协议基础知识