当前位置:网站首页>Array to linked list
Array to linked list
2022-07-28 06:53:00 【Xiao Qiao】
Use Node Represents a linked list node
public class Node {
public int val;
public Node next=null;
public Node(int val) {
this.val = val;
}
@Override
public String toString() {
return "["+val+"]";
}
}Main method
public class mmain {
public static void main(String[] args) {
// Create array
int[] array={1,2,3,4,5};
// Node head=arrayTolinkedList(array);
Node head=arrayTolinkedList2(array);
print(head);
}Method 1 ( Without puppet node )
public static Node arrayTolinkedList(int[] array){
// Traversal array , Insert the elements in sequence
// If every tail insertion , You need to know the end node of the current linked list
// You can directly use a reference to remember the tail node
//head It is the reference of the header node , In the initial case , The list is empty
Node head=null;
//tail It's also empty
Node tail=null;
for (int x:array){
Node node=new Node(x);
// hold Node Perform tail insertion
// You need to determine whether the current linked list is empty
if (head==null){
head=node;
tail=node;
}else{
// When the linked list is not empty , Then insert new , Don't worry head 了 , Direct manipulation tail That's it
tail.next=node;
// Once the insertion is complete , The new node becomes tail, You need to update tail The direction of
tail=tail.next;
}
}
return head;
}
Method 2 ( With puppet node )
Do it in a simpler way , If we create a puppet node , Subsequent tail insertion operation , There is no need to discuss it in two cases .
public static Node arrayTolinkedList2(int[] array) {
Node head=new Node(0);// Puppet node
// Also create a tail Reference to save the tail of the linked list
Node tail=head;
for (int x:array){
Node node=new Node(x);
tail.next=node;
tail=tail.next;
}
// The requirement of the code is that the returned result is a linked list without dummy nodes
// If you return head,head It points to the puppet node , Unqualified .
return head.next;
}
Print function
public static void print(Node head) {
for (Node cur=head;cur!=null;cur=cur.next){
System.out.println(cur.val);
}
}
// public static void print(Node node) {
// while (node != null) {
// System.out.println(node.val);
// node = node.next;
// }
}
边栏推荐
- Code tidiness (I)
- How to calculate the size of structure, segment and Consortium (common body)
- It is recommended to wear air conduction earphones, which do not need to wear in ear
- 链表中结点的插入和删除
- Pku-2739-sum of constructive prime numbers
- @Postconstruct annotations and useful examples
- QGraphicsView提升为QChartView
- Mongo SSL configuration practice
- Analysis of the semaphore source code of AQS
- 技术分享 | 使用postman发送请求
猜你喜欢

技术分享 | 接口测试常用代理工具

Mongodb replica set and partitioned cluster

如何描述一个BUG以及BUG级别的定义、生命周期

raid磁盘阵列

Technology sharing | how to simulate real use scenarios? Mock technology to help you

iptables防火墙

Which is the best and most cost-effective air conduction headset recommended

KVM热迁移

Lancher deployment practice

C language memcpy library functions and the role of memmove
随机推荐
DHCP原理与配置
技术分享 | 使用 cURL 发送请求
DNS正向解析实验
Code tidiness (I)
Question brushing record -- binary tree
raid磁盘阵列
Qgraphicsview promoted to qchartview
测试面试题集锦(二)| 测试工具篇(附答案)
SSH service configuration
How to calculate the size of structure, segment and Consortium (common body)
MySQL主从
Question brushing record - linked list
Elastic common high frequency commands
Which brand of air conduction earphones is good and highly praised
思寒漫谈测试人职业发展
Rain Scene Effect (I)
Initializingbean interface and examples
[C language] custom structure type
Test interview questions collection (II) | test tools (with answers)
Centos7 deploy MySQL database server