Skip to content

Instantly share code, notes, and snippets.

@AahanSingh
Created Jul 4, 2021
Embed
What would you like to do?
CList DeleteAtP
func DeleteAtP(head **Node, p int) {
if p < 1 {
fmt.Println("\nPositions start from 1. Cannot delete.")
return
}
if *head == nil {
fmt.Println("\nList is empty")
return
}
if p == 1 {
DeleteFirst(head)
return
}
i := 1
var previous *Node = nil
current := *head
for i < p && current.Next != *head {
previous = current
current = current.Next
i++
}
if i < p {
fmt.Println("Invalid position.")
return
}
fmt.Println("Deleting", current, "at position", p)
previous.Next = current.Next
current = nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment