Skip to content

Instantly share code, notes, and snippets.

@AahanSingh
Created July 4, 2021 07:20
Show Gist options
  • Save AahanSingh/5c09881bde29fd48d3cd1873936d2733 to your computer and use it in GitHub Desktop.
Save AahanSingh/5c09881bde29fd48d3cd1873936d2733 to your computer and use it in GitHub Desktop.
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