Delete middle node from linked list in go

Go program for Delete middle node from linked list. Here problem description and explanation.

package main
import "fmt"
/*
    Go program for
    Delete middle node of linked list
*/
// 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}
}
// Adding new node at beginning of linked list
func(this *SingleLL) addNode(data int) {
    // Create new node
    var node * LinkNode = getLinkNode(data)
    // Connect current node to previous head
    node.next = this.head
    this.head = 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) {
        // Display node value
        fmt.Print( temp.data, " → ")
        // Visit to next node
        temp = temp.next
    }
    fmt.Print(" NULL\n")
}
// Delete the middle node of linked list
func(this SingleLL) deleteMid() {
    if this.head == nil {
        // When linked list are no elements
        fmt.Println("Empty Linked List")
    } else if this.head.next == nil && 
        this.head.next.next == nil {
        // When linked list are less than  of 3 nodes
        fmt.Println("Less then 3 node of linked list")
    } else {
        var temp * LinkNode = this.head
        var midPrevious * LinkNode = nil
        // Find the middle and its previous node
        for (temp != nil && temp.next != nil && 
            temp.next.next != nil) {
            if midPrevious == nil {
                // When first node
                midPrevious = temp
            } else {
                // Visit to next node
                midPrevious = midPrevious.next
            }
            // Visit to next second next node
            temp = temp.next.next
        }
        // Get the node which is deleting 
        temp = midPrevious.next
        // Show deleted node
        fmt.Println("Delete node : ", temp.data)
        // Change link
        midPrevious.next = temp.next
    }
}
func main() {
    var sll * SingleLL = getSingleLL()
    // Linked list
    // 7 → 6 → 5 → 4 → 3 → 2 → 1 → NULL
    sll.addNode(1)
    sll.addNode(2)
    sll.addNode(3)
    sll.addNode(4)
    sll.addNode(5)
    sll.addNode(6)
    sll.addNode(7)
    fmt.Println("Before Delete middle node")
    sll.display()
    // Delete middle node
    sll.deleteMid()
    fmt.Println("After Delete middle node")
    sll.display()
}

Output

Before Delete middle node
 7 → 6 → 5 → 4 → 3 → 2 → 1 → NULL
Delete node : 4
After Delete middle node
 7 → 6 → 5 → 3 → 2 → 1 → NULL


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