Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当head.next==null时,为什么head.data=0?_Java_Linked List - Fatal编程技术网

Java 当head.next==null时,为什么head.data=0?

Java 当head.next==null时,为什么head.data=0?,java,linked-list,Java,Linked List,从事硬件作业。赋值是递归地反转链表。我只是不明白为什么在我的System.out.println(head.data)行head.data总是打印为0。假设我输入4,5,6(这项工作的填充和读取方法,如果我在期末考试中打印,它将显示head仍然是4,5,6)和head。接下来的是null,这不意味着head.data等于6?我知道我在试图扭转这一局面时遇到了很多其他问题,但我不明白为什么head.next为nullhead.data为0。我想head。next是列表中的下一项,而不是当前项 pu

从事硬件作业。赋值是递归地反转链表。我只是不明白为什么在我的
System.out.println(head.data)
head.data
总是打印为
0
。假设我输入
4,5,6
(这项工作的填充和读取方法,如果我在期末考试中打印,它将显示head仍然是4,5,6)和
head。接下来的
null
,这不意味着
head.data
等于
6
?我知道我在试图扭转这一局面时遇到了很多其他问题,但我不明白为什么
head.next
null
head.data
为0。我想
head。next
是列表中的下一项,而不是当前项

public static Node reverse(Node head)
{
    Node n = new Node();
    if (head == null)
    {
        return n;
    } else if (head.next == null)
    {
        System.out.println(head.data);
        n.data = head.data;
        head.next = null;
        n.next = reverse(head.next);
        return n;
    } else
    {
        reverse(head.next);
        return n;
    }
}
以下是:

Node n = new Node();
if(head == null) {
    return n;
}
结果与空列表相反,空列表是一个元素列表,默认值为
data
(可能为零?)

另外,最后的
else
显然是不对的,因为它在返回之前从不对
n
做任何事情