当前位置:网站首页>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;
// }
}
边栏推荐
- Which brand of air conduction earphones is good and highly praised
- rancher部署实战
- HDU-5783 Divide the Sequence(贪心水题)
- 数组转链表
- Hdu-5805-nanoape loves sequence (thinking questions)
- 单项链表的创建、遍历以及按要求查找结点
- 测试人生 | 二线城市年薪超40W?疫情之下涨薪100% + 是怎么做到的?
- Dynamic memory management function of C language
- PKU-2739-Sum of Consecutive Prime Numbers(筛素数法打表)
- @Postconstruct annotations and useful examples
猜你喜欢

SSH服务配置

Analysis of cyclicbarrier source code of AQS

It is recommended to wear air conduction earphones, which do not need to wear in ear

MySQL index optimization

软件开发中常见模型

DHCP原理与配置
![[C language] dynamic memory management](/img/bb/2ec65b38e85f53269dc03d885d70f4.png)
[C language] dynamic memory management

Prometheus monitoring Nacos

Which is the best air conduction Bluetooth headset? Air conduction Bluetooth headset ranking

技术分享 | 实战详解接口测试请求方式Get、post
随机推荐
NFS 共享存储服务
Hdu-2036-reform spring breeze blowing all over the ground (polygon area template)
It is recommended to wear air conduction earphones, which do not need to wear in ear
技术分享 | 接口测试价值与体系
小tips
MySQL主从
DNS正向解析实验
测试面试题集锦(五)| 自动化测试与性能测试篇(附答案)
Graphic pipeline foundation (I)
KVM hot migration
技术分享 | 使用 cURL 发送请求
Analysis of the semaphore source code of AQS
Redis implementation of distributed lock and analysis of the main process of redismission distributed lock
What kind of air conduction Bluetooth headset with good configuration is recommended
MySQL master master
Lancher deployment practice
Build php7 private warehouse
测试面试题集锦(一)| 软件测试常见必考问题与流程篇(附答案)
[C language] custom structure type
DNS域名解析服务