Skip to content

Instantly share code, notes, and snippets.

@AahanSingh
Last active June 30, 2021 14:31
Embed
What would you like to do?
Deleting the last node in the list.
// DeleteLast deletes the last node in the linkedlist.
// Deletion is inplace
func DeleteLast(head **Node) {
if *head == nil {
fmt.Println("\nList is empty")
return
}
// If there is only 1 node in the list
if (*head).Next == nil {
*head = nil
return
}
current := (*head).Next
previous := *head
for ; current.Next != nil; current = current.Next {
previous = current
}
previous.Next = nil
fmt.Println("\nDeleting last node: ", current)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment