英文:
runtime error: invalid memory address or nil pointer dereference with loop
问题
我正在尝试解决这个问题:https://leetcode.com/problems/reverse-linked-list/description/?envType=study-plan&id=level-1
输入:head = [1,2,3,4,5]
这是我的代码:
func reverseList(head *ListNode) *ListNode {
var prev *ListNode
prev = nil
for head.Next != nil {
head, head.Next, prev = head.Next, prev, head
}
if head.Next == nil {
head.Next = prev
}
return head
}
运行后,程序报错:panic: runtime error: invalid memory address or nil pointer dereference.
请解释一下为什么会出现这个错误。
使用输入:head = [1,2,3,4,5]。我希望循环4次,直到head.Next == nil。但是出现了我意料之外的情况。
英文:
I am trying to solve this problem: https://leetcode.com/problems/reverse-linked-list/description/?envType=study-plan&id=level-1
Input: head = [1,2,3,4,5]
This is my code:
func reverseList(head *ListNode) *ListNode {
var prev *ListNode
prev = nil
for head.Next != nil {
head, head.Next, prev = head.Next, prev, head
}
if head.Next == nil {
head.Next = prev
}
return head
}
After running, the program gives an error: panic: runtime error: invalid memory address or nil pointer dereference.
Please explain to me why this error occurs
With Input: head = [1,2,3,4,5]. I hope to do the loop 4 times until head.Next == nil. But something not as I expected happened.
答案1
得分: 1
如果头节点是空的 -> head.Next会出现错误,就像你给出的那样
func reverseList(head *ListNode) *ListNode {
var prev *ListNode
prev = nil
for head != nil && head.Next != nil {
head, head.Next, prev = head.Next, prev, head
}
if head != nil && head.Next == nil {
head.Next = prev
}
return head
}
更简单的方式是:
func reverseList(head *ListNode) *ListNode {
var prev *ListNode
prev = nil
for head != nil {
head, head.Next, prev = head.Next, prev, head
}
if head == nil {
head = prev
}
return head
}
英文:
if head is nil -> head.Next will be error like you given
func reverseList(head *ListNode) *ListNode {
var prev *ListNode
prev = nil
for head != nil && head.Next != nil {
head, head.Next, prev = head.Next, prev, head
}
if head != nil && head.Next == nil {
head.Next = prev
}
return head
}
and more simple is :
func reverseList(head *ListNode) *ListNode {
var prev *ListNode
prev = nil
for head != nil {
head, head.Next, prev = head.Next, prev, head
}
if head == nil {
head = prev
}
return head
}
答案2
得分: 0
错误出现在for head.Next != nil
这一行,因为如果head
是nil
,那么你就在解引用一个空指针,这会导致恐慌。
为了避免这种情况,你需要检查head
是否为nil
。
你的代码可以像这样写:
func reverseList(head *ListNode) *ListNode {
var prev *ListNode
prev = nil
if head != nil {
for head.Next != nil {
head, head.Next, prev = head.Next, prev, head
}
if head.Next == nil {
head.Next = prev
}
}
return head
}
英文:
The error comes from for head.Next != nil
, because if head is nil then you're dereferencing a nil pointer, which results in the panic.
To avoid this, you need to include a check that head
is not nil.
Your code could look something like this:
func reverseList(head *ListNode) *ListNode {
var prev *ListNode
prev = nil
if head != nil {
for head.Next != nil {
head, head.Next, prev = head.Next, prev, head
}
if head.Next == nil {
head.Next = prev
}
}
return head
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论