Remove every k th node of the linked list in go

Go program for Remove every k th node of the linked list. Here problem description and explanation.

package main
import "fmt"
// Go Program for
// delete every k-th node of the 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}
}
// Add new node at the end of linked list
func(this *SingleLL) addNode(value int) {
    // Create  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.Println("Empty Linked list\n")
    }
}
func(this *SingleLL) removeKh(kth int) {
    if this.head == nil {
        // When no element
        fmt.Println("Empty Linked list")
    } else if kth <= 0 {
        fmt.Println("\nInvalid position ", kth)
    } else {
        var temp * LinkNode = this.head
        var hold * LinkNode = nil
        var checker * LinkNode = nil
        var count int = 0
        if kth == 1 {
            // When delete all element
            this.head = nil
        }
        for (temp != nil) {
            count++
            if count % kth == 0 {
                // When delete node found
                hold = temp
            } else {
                // Get current node
                checker = temp
            }
            temp = temp.next
            // When get free node
            if hold != nil {
                if checker != nil {
                    // Unlink node
                    checker.next = hold.next
                }
                hold = nil
            }
        }
    }
}
func main() {
    var sll * SingleLL = getSingleLL()
    // Linked list
    // 1 → 2 → 3 → 4 → 5 → 6 → 7 → NULL
    sll.addNode(1)
    sll.addNode(2)
    sll.addNode(3)
    sll.addNode(4)
    sll.addNode(5)
    sll.addNode(6)
    sll.addNode(7)
    var position int = 2
    fmt.Println("Position : ", position)
    fmt.Println("Before Delete Linked List")
    sll.display()
    sll.removeKh(position)
    fmt.Println("\n After Linked List")
    sll.display()
}

Output

Position : 2
Before Delete Linked List
  1  2  3  4  5  6  7
 After Linked List
  1  3  5  7


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