Java 如何在不使用head的情况下打印链表的元素

Java 如何在不使用head的情况下打印链表的元素,java,list,nodes,Java,List,Nodes,我试图在不使用head节点的情况下重写这个程序,有什么帮助吗 我尝试了getFirst()方法来设置指向链表第一个元素的指针,但没有成功。是否有任何方法可以将链表的第一个值设置为新的头,而不实际创建值为0的头 class Node { int value; Node next; public Node(int v) { //gives the node a value value = v; } } public class HelloWorl

我试图在不使用head节点的情况下重写这个程序,有什么帮助吗

我尝试了
getFirst()
方法来设置指向链表第一个元素的指针,但没有成功。是否有任何方法可以将链表的第一个值设置为新的头,而不实际创建值为
0
的头

class Node {


int value;
    Node next;

    public Node(int v) {   //gives the node a value
        value = v;
    }
}


public class HelloWorld {
    public static void main (String[] args) throws Exception {

        int[] array = {1, 2, 5, 3, 0, 8};
        Node head = new Node(0);
        Node current = head;

        for (int i = 0; i < array.length; i++) {
            // 2. create each node object
            Node node = new Node(array[i]);
            current.next = node;  
            current = node; 
        }

        current = head.next; 
        while (current != null) {  
            System.out.println(current.value);
            current = current.next;
        }
    }
}
类节点{
int值;
节点下一步;
公共节点(intv){//给节点一个值
值=v;
}
}
公共类HelloWorld{
公共静态void main(字符串[]args)引发异常{
int[]数组={1,2,5,3,0,8};
节点头=新节点(0);
节点电流=头;
for(int i=0;i
是的,你可以。只需使用数组的第一个元素初始化头部。

public class HelloWorld {
    public static void main (String[] args) throws Exception {

        int[] array = {1, 2, 5, 3, 0, 8};

        // Initialize head with first element of array. 
        Node head = new Node(array[0]);
        Node current = head;

        for (int i = 1; i < array.length; i++) {
            // 2. create each node object
            Node node = new Node(array[i]);
            current.next = node;  
            current = node; 
        }

        current = head; 
        while (current != null) {  
            System.out.println(current.value);
            current = current.next;
        }
    }
}
公共类HelloWorld{
公共静态void main(字符串[]args)引发异常{
int[]数组={1,2,5,3,0,8};
//用数组的第一个元素初始化头部。
节点头=新节点(数组[0]);
节点电流=头;
for(int i=1;i
您可以在第一次迭代中初始化您的头部。下面是一个简单的代码:

class Node {
    int value;
    Node next;

    public Node(int v) {   //gives the node a value
        value = v;
    }
}


class HelloWorld {
    public static void main (String[] args) throws Exception {

        int[] array = {1, 2, 5, 3, 0, 8};
        Node head = null, current = null;

        for (int i = 0; i < array.length; i++) {
            if(head == null){
                head = new Node(array[i]);
                current = head;
            }
            else{
                current.next = new Node(array[i]);
                current = current.next;
            }
        }

        current = head; 
        while (current != null) {  
            System.out.println(current.value);
            current = current.next;
        }
    }
}
类节点{
int值;
节点下一步;
公共节点(intv){//给节点一个值
值=v;
}
}
类HelloWorld{
公共静态void main(字符串[]args)引发异常{
int[]数组={1,2,5,3,0,8};
节点头=null,当前=null;
for(int i=0;i

我强烈建议您学习一些关于算法和数据结构的课程,以增强您的基础知识。这是一个好的开始:

为什么不从
数组中读取第一个元素,并将其放入
新节点中呢?