当前位置:网站首页>Merge two ordered linked lists ---2022/02/24
Merge two ordered linked lists ---2022/02/24
2022-06-11 17:34:00 【City Pig】
List of articles
Title Description
Merge two ascending linked lists into a new Ascending Link list and return . The new linked list is made up of all the nodes of the given two linked lists .
Title source
Problem solving thinking
Ideas :
The first method : Add a new linked list while comparing ( There is a problem of comparing boundaries ) But the time complexity here is n The square of . Method description to determine whether the linked list is valid before adding , If linked list 1(2) Has pointed to the end , Put the linked list 2(1) All the remaining elements are added to the new linked list , And return directly . If linked list 1 The current element < Linked list 2 The current element , Linked list 1 The current element is added to the new linked list , Linked list 1 Point to the next element , Otherwise the linked list 2 The current element is added to the new linked list , Linked list 2 Point to the next element
The second method : Take out the linked list respectively 1 And the list 2 Put your data in list( Add : It is better not to choose list, Because the sorting interface is called only for arrays ) Inside , And then sort it , After sorting , And then add them to the new linked list one by one , Return to the header node . The time complexity is n. And the operation is more concise .
Knowledge supplement
1、list、set、map The difference between .
And how to create .
establish list
List <> a=new ArrayList<>();
establish set
Set <> a=new LinkedHashSet<>();
establish map
Map <> a=new HashMap<>();
2、 How to add a node method outside the method ( Namely cur and head I am still a little confused about my current relationship )
Code implementation
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;
// Because the node class does not define a method for adding nodes after it
// So I keep making circles here , I can't walk out ....
// Then it was solved , But still not very clear ,head and curent How to match ?
while(i<j)
{
ListNode newNode = new ListNode(temp1[i]);
curent.next = newNode;
curent=curent.next;
i++;
}
return head;
}
}
Performance evaluation

It is still a trade-off between memory and time .
边栏推荐
- ffmpeg硬编解码 Inter QSV
- Service学习笔记02-实战 startService 与bindService
- vscode配置eslint自动格式化报错“Auto Fix is enabled by default. Use the single string form“
- Read and understand the development plan for software and information technology service industry during the "14th five year plan"
- 论文阅读 dyngraph2vec: Capturing Network Dynamics using Dynamic Graph Representation Learning
- Typescipt Basics
- 6-1 需要多少单词可以组成一句话?
- Sohu tout le personnel a été escroqué, quels problèmes ont été exposés?
- 如何成为一个乐观派组织?
- which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mod
猜你喜欢
随机推荐
Mathematical foundations of information security Chapter 3 - finite fields (I)
Talk about the interview questions of the collection
Tidb CDC create task error unknown or incorrect time zone
Sohu tout le personnel a été escroqué, quels problèmes ont été exposés?
6-8 有结构文件的读写1
Authing 双周动态:Authing 论坛上线(4.25-5.8)
tidb-cdc日志tables are not eligible to replicate
What problems are exposed when all Sohu employees are cheated?
活动 | Authing 首次渠道合作活动圆满落幕
6-3 读文章(*)
Mathematical basis of information security Chapter 2 - congruence
R language to find missing value location of data set
Go get downloaded package path
删除链表的倒数第N个节点---2022/02/22
CLP information -5 keywords to see the development trend of the financial industry in 2022
Axi protocol Basics
Service学习笔记02-实战 startService 与bindService
Go path: goroot & gopath
vscode配置eslint自动格式化报错“The setting is deprecated. Use editor.codeActionsOnSave instead with a source“
mysql 大表的拆分方式









