Skip to content

Instantly share code, notes, and snippets.

@AahanSingh
Created July 4, 2021 07:20
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
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