当前位置:网站首页>合并两个有序链表---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;
}
}
性能评估

依旧是以内存换取时间。
边栏推荐
- A simple understanding of closures
- mysql 大表的拆分方式
- Is the second-class cost engineer worth the exam? What is the development prospect?
- Authing CEO 谢扬入选福布斯 2021 年 30 Under 30 亚洲榜单
- 从制造到“智造”,探索制造企业破局之道
- Guide to Dama data management knowledge system: percentage of chapter scores
- 信息安全数学基础 Chapter 3——有限域(一)
- Derivation of child numbering formula for nodes numbered I in full k-ary tree
- 我的CのERROR们
- Hash表、 继承
猜你喜欢

用实际案例分析PMP与ACP应该考哪个?哪个更有用?

Activity | authing's first channel cooperation activity came to a successful conclusion

Authing 双周动态:Authing 论坛上线(4.25-5.8)
![[MySQL] detailed explanation of redo log, undo log and binlog (4)](/img/67/6e646040c1b941c270b3efff74e94d.png)
[MySQL] detailed explanation of redo log, undo log and binlog (4)

Katalon Studio Enterprise

How to become an optimist organization?

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

使用exe4j 将.jar文件打包为.exe文件

Sohu tout le personnel a été escroqué, quels problèmes ont été exposés?

论文阅读 dyngraph2vec: Capturing Network Dynamics using Dynamic Graph Representation Learning
随机推荐
信息安全数学基础 Chapter 3——有限域(二)
Cocoapod only updates the specified library (does not update the index)
Leetcode-- array
How to become an optimist organization?
RecyclerView缓存复用解析,源码解读
05_特征工程—降维
Qlineedit set input mask
What subclasses inherit, polymorphism, and upward transformation
【线上问题】Timeout waiting for connection from pool 问题排查
Typescript learning notes (II)
Song of the sea in 5g Era
6-2 写文章(*)
CLP information -5 keywords to see the development trend of the financial industry in 2022
Authing Share|理解 SAML2 协议
[MySQL] detailed explanation of redo log, undo log and binlog (4)
spawn ./gradlew EACCES at Process.ChildProcess._handle.onexit
JSP page initial loading method
Create database instance
Vscode configures eslint to automatically format an error "auto fix is enabled by default. use the single string form“
ffmpeg CBR精准码流控制三个步骤