Insert node at end of linked list in go

Write a program which is create and add linked list node at the end (tail, last) position in golang.

Suppose we are inserted the following (10, 20, 30 ,40 ,50) node in a sequence.

insert node at end of linked list
package main
import "fmt"
// Go Program for
// Insert linked list element at end position

// Linked list node
type LinkNode struct {
    data int
    next * LinkNode
}
func getLinkNode(data int) * LinkNode {
   
    return &LinkNode {data,nil}
}
type SingleLL struct {
    head * LinkNode
}
func getSingleLL() * SingleLL {
    return &SingleLL {nil}
}
// Add new node at the end of linked list
func(this *SingleLL) addNode(value int) {
    // Create new node
    var node * LinkNode = getLinkNode(value)
    if this.head == nil {
        this.head = node
    } else {
        var temp * LinkNode = this.head
        // find last node
        for (temp.next != nil) {
            // Visit to next node
            temp = temp.next
        }
        // Add node at last position
        temp.next = node
    }
}
//Display all Linked List elements
func(this SingleLL) display() {
    if this.head != nil {
        var temp * LinkNode = this.head
        for (temp != nil) {
            // Display node value
            fmt.Print("  ", temp.data)
            // Visit to next node
            temp = temp.next
        }
    } else {
        fmt.Print("Empty Linked list\n")
    }
}
func main() {
    var sll * SingleLL = getSingleLL()
    // Linked list
    // 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
    sll.addNode(1)
    sll.addNode(2)
    sll.addNode(3)
    sll.addNode(4)
    sll.addNode(5)
    sll.addNode(6)
    sll.addNode(7)
    sll.addNode(8)
    fmt.Print(" Linked List \n")
    sll.display()
}
 Linked List
  1  2  3  4  5  6  7  8

Time complexity of above program is O(n). We can optimize above algorithm using one extra pointer. Which is hold the reference of last node. Below are implementation of this logic.

package main
import "fmt"
// Go Program for
// Insert linked list element at end position set B

// Linked list node
type LinkNode struct {
    data int
    next * LinkNode
}
func getLinkNode(data int) * LinkNode {
    return &LinkNode {data,nil}
}
type SingleLL struct {
    head * LinkNode
    tail * LinkNode
}
func getSingleLL() * SingleLL {
    return &SingleLL {nil,nil}
}
// Add new node at the end of linked list
func(this *SingleLL) addNode(value int) {
    // Create a new node
    var node * LinkNode = getLinkNode(value)
    if this.head == nil {
        this.head = node
    } else {
        this.tail.next = node
    }
    this.tail = node
}
// Display linked list element
func(this SingleLL) display() {
    if this.head == nil {
        return
    }
    var temp * LinkNode = this.head
    // iterating linked list elements
    for (temp != nil) {
        fmt.Print( temp.data, " → ")
        // Visit to next node
        temp = temp.next
    }
    fmt.Print("NULL\n")
}
func main() {
    var sll * SingleLL = getSingleLL()
    // Linked list
    // 10 → 20 → 30 → 40 → 50 → NULL
    sll.addNode(10)
    sll.addNode(20)
    sll.addNode(30)
    sll.addNode(40)
    sll.addNode(50)
    fmt.Print("Linked List \n")
    sll.display()
}
 Linked List
 10 → 20 → 30 → 40 → 50 → NULL

Time complexity of above program is O(1).



Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved