duanben1909 2018-04-26 22:19
浏览 66
已采纳

Golang指针反向链接列表混淆

I'm new to Golang and am a bit confused as to how pointers work here and I'm using a reverse linked list question as an example.

func reverseList(head *ListNode) *ListNode {
  var prev *ListNode = nil
  for {
    if head == nil {
      break
    }
    temp := head
    head = head.Next
    temp.Next = prev
    prev = temp
  }
    return prev
}

In this case, temp and head is pointing to the same memory location. But if I were to put the line temp.Next = prev before head = head.Next, the head.Next will point to nil.

What is happening under the hood when we say temp.Next = prev. Are we saying that the structure temp is pointing at is now changed and thus if I were to put that line above head = head.Next then head is now pointing to this changed structure as well?

I thought in order to change the actual value of head.Next, we would have to dereference it?

I'm a little bit confused as to why this wouldn't work instead

func reverseList(head *ListNode) *ListNode {
  var prev *ListNode = nil
  for {
    if head == nil {
      break
    }
    temp := head   
    temp.Next = prev
    prev = temp
    head = head.Next <--- CHANGED ORDERING HERE
  }
    return prev
}
  • 写回答

1条回答 默认 最新

  • dongzhenjian5195 2018-04-26 22:26
    关注

    It wouldn't work because you're invalidating head on the first iteration. Here's the flow:

    temp := head   
    // Since temp is a pointer, any modifications made to it will also
    // impact head. So when you set temp.Next here, you're also setting
    // head.Next. Since prev is always nil on the first iteration, you've
    // set head.Next = nil.
    temp.Next = prev
    prev = temp
    // This will set head to nil, always.
    head = head.Next
    

    In the correct version, updating head = head.Next before any writes happen to it means you've moved on to the next element already and so it is safe to overwrite the value.

    Effectively what the second version does is snip off all elements of the list except the first one. If you didn't make a reference to the second element before calling this function, then they are now off in limbo and will get garbage-collected.

    Updates based on comments:
    When you do something like temp := head, you are saying "create a pointer called temp and point it to the same thing that head points to." If you then modify the thing that temp is pointing to (like for example, doing temp.Next = prev) you will also see the changes when reading the data that head points to since it is still pointing at the same place.

    When you then do head = head.Next you are saying "update head to point to wherever head.Next is pointing." This updates head itself instead of the data that it is pointing to, so you won't see any changes related to temp.

    Here is a good resource to learn more about pointers: https://dave.cheney.net/2017/04/26/understand-go-pointers-in-less-than-800-words-or-your-money-back

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀