A
A
Artem Rogozin2019-11-08 00:53:21
go
Artem Rogozin, 2019-11-08 00:53:21

How to deploy a linked list in golang?

I don't understand how to expand the linked list. I'm already completely confused.

type LinkedList struct {
  name   string
  length int
  head   *ItemLL
}

type ItemLL struct {
  value string
  next  *ItemLL
}

func (l *LinkedList) Reverse() {

  fmt.Println(l.head)
  fmt.Println(l.head.next)
  fmt.Println(l.head.next.next)

  var top *ItemLL

  for l.head != nil {
    buf := l.head.next
    l.head.next = top
    top = l.head
    l.head = buf
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2019-11-08
@syberianv

About the same as in any language. I don't know, so sorry.

func (l *LinkedList) Reverse() {

  var cursor *ItemLL := l.head
  var prev *ItemLL := nil

  for cursor != nil {
    var next := cursor.next
    cursor.next := prev
    prev := cursor
    cursor := next
  }
   l.head := prev
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question