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

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

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效