HW 할당 작업. 이 할당은 링크 된 목록을 재귀 적으로 역전시키는 것입니다. 나는 왜 내 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
이 현재 목록이 아니라 다음 항목이었을 것이라고 생각했다. 하나. 다음head.next == null 일 때 head.data가 0 인 이유는 무엇입니까?
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;
}
}