爪哇为什么是头。数据=0时,头。next==null?
做硬件任务。任务是递归地反转链表。我只是不明白为什么在我的System.out.println(head.data)
行head.data
总是打印成0
。假设我输入4,5,6
(这项工作的填充和读取方法,如果我在期末考试中打印,它将显示头仍然是4,5,6)和head.next
是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;
}
}
共 (0) 个答案