-
-
Save AahanSingh/a690ddae1b469c9ad27315b6e014b4fc to your computer and use it in GitHub Desktop.
Deleting the last node in the list.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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