运行时错误:循环中出现无效的内存地址或空指针解引用。

huangapple go评论79阅读模式
英文:

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这一行,因为如果headnil,那么你就在解引用一个空指针,这会导致恐慌。

为了避免这种情况,你需要检查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
}

huangapple
  • 本文由 发表于 2023年1月6日 15:47:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75028256.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定