Skip to content

Instantly share code, notes, and snippets.

@AahanSingh
Last active June 30, 2021 14:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AahanSingh/a690ddae1b469c9ad27315b6e014b4fc to your computer and use it in GitHub Desktop.
Save AahanSingh/a690ddae1b469c9ad27315b6e014b4fc to your computer and use it in GitHub Desktop.
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