如何使用递归方法指针在Golang中获取链表的长度?

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

how to get length of linked list in golang with recursive of method pointer?

问题

我有一个单链表。如何使用具有指针接收器的递归方法获取链表的长度?

  1. type Node struct {
  2. data int
  3. next *Node
  4. }

我尝试了这样的方法,但总是返回1。

  1. func (n *Node) recursiveLength() (result int) {
  2. if n != nil {
  3. result += 1
  4. n = n.next
  5. }
  6. return
  7. }
英文:

I have a single linked list. how do I get the length of the linked list with a recursive method that had a pointer receiver?

  1. type Node struct {
  2. data int
  3. next *Node
  4. }

I had tried like this, but always return 1

  1. func (n *Node) recursiveLength() (result int) {
  2. if n != nil {
  3. result += 1
  4. n = n.next
  5. }
  6. return
  7. }

答案1

得分: 1

你的解决方案不是递归的。它有编译错误。但是如果我们想要修复它,可以像这样修改:

  1. package main
  2. import "fmt"
  3. type Node struct {
  4. data int
  5. next *Node
  6. }
  7. func (n *Node) recursiveLength() (result int) {
  8. if n != nil {
  9. result += 1
  10. n = n.next
  11. return result + n.recursiveLength()
  12. }
  13. return 0
  14. }
  15. func main() {
  16. x := Node{data: 0, next: &Node{data: 1, next: &Node{data: 2, next: nil}}}
  17. fmt.Println(x.recursiveLength())
  18. }

但是这种写法不是一个好主意,更好的做法是将其改为一个接受 Node 并返回其长度的函数:

  1. package main
  2. import "fmt"
  3. type Node struct {
  4. data int
  5. next *Node
  6. }
  7. func recursiveLength(n *Node) (result int) {
  8. if n != nil {
  9. n = n.next
  10. return 1 + recursiveLength(n)
  11. }
  12. return 0
  13. }
  14. func main() {
  15. x := Node{data: 0, next: &Node{data: 1, next: &Node{data: 2, next: nil}}}
  16. fmt.Println(recursiveLength(&x))
  17. }
英文:

Your solution is not recursive. It's have compilation error. But if we want to fix it it could be like this:

  1. package main
  2. import "fmt"
  3. type Node struct {
  4. data int
  5. next *Node
  6. }
  7. func (n *Node) recursiveLength() (result int) {
  8. if n != nil {
  9. result += 1
  10. n = n.next
  11. return result + n.recursiveLength()
  12. }
  13. return 0
  14. }
  15. func main() {
  16. x := Node{data: 0, next: &Node{data: 1, next: &Node{data: 2, next: nil}}}
  17. fmt.Println(x.recursiveLength())
  18. }

But this is not a good idea to write length method, It's better to change it to a function that accepts a Node and returns its length:

  1. package main
  2. import "fmt"
  3. type Node struct {
  4. data int
  5. next *Node
  6. }
  7. func recursiveLength(n *Node) (result int) {
  8. if n != nil {
  9. n = n.next
  10. return 1 + recursiveLength(n)
  11. }
  12. return 0
  13. }
  14. func main() {
  15. x := Node{data: 0, next: &Node{data: 1, next: &Node{data: 2, next: nil}}}
  16. fmt.Println(recursiveLength(&x))
  17. }

huangapple
  • 本文由 发表于 2022年1月20日 23:16:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/70788754.html
匿名

发表评论

匿名网友

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

确定