Reverse Link List

We have given task for Reverse Link List as-

package main
 
import "fmt"
 
type Node struct {
    prev *Node
    next *Node
    key  interface{}
}
 
type List struct {
    head *Node
    tail *Node
}
 
func (L *List) Insert(key interface{}) {
    list := &Node{
        next: L.head,
        key:  key,
    }
    if L.head != nil {
        L.head.prev = list
    }
    L.head = list
 
    l := L.head
    for l.next != nil {
        l = l.next
    }
    L.tail = l
}
 
func (l *List) Display() {
    list := l.head
    for list != nil {
        fmt.Printf("%+v ", list.key)
        list = list.next
    }
    fmt.Println()
}
 
func Display(list *Node) {
    for list != nil {
        fmt.Printf("%v ", list.key)
        list = list.next
    }
    fmt.Println()
}
 
func ShowBackwards(list *Node) {
    for list != nil {
        fmt.Printf("%v ", list.key)
        list = list.prev
    }
    fmt.Println()
}
 
func (l *List) Reverse() {
    curr := l.head
    var prev *Node
    l.tail = l.head
 
    for curr != nil {
        next := curr.next
        curr.next = prev
        prev = curr
        curr = next
    }
    l.head = prev
    Display(l.head)
}
 
func main() {
    link := List{}

    fmt.Printf("Head: %v\n", link.head.key)
    fmt.Printf("Tail: %v\n", link.tail.key)
    link.Display()
    fmt.Printf("head: %v\n", link.head.key)
    fmt.Printf("tail: %v\n", link.tail.key)
    link.Reverse()
}

Please tell us what issues the code produces.

Is the output different from what you expect? Does it error during compilation? Something else?

How? Can you share the output of the error?

Help us to help you! Not everyone does read the forum when they have a compiler around to actually try and compile your code…

Issue is been resolved after we had modified code as -

func main() {
        var n int
    _, err := fmt.Scanln(&n)
    if err != nil || n < 0 {
        return
    }
    link := List{}
    s := make([]int, n)
    for i := range s {
        fmt.Scanln(&s[i])
        link.Insert(s[i])
    }
    fmt.Printf("Head: %v\n", link.head.key)
    fmt.Printf("Tail: %v\n", link.tail.key)
    link.Display()
    fmt.Printf("head: %v\n", link.tail.key)
    fmt.Printf("tail: %v\n", link.head.key)
    link.Reverse()
}

Well, copy/pasting via mobile isn’t nice either.

It’s easier to help if all necessary data is available without requiring extra work from those who want to help.

And that is even giving a panic at runtime, not a compile error.

1 Like