Skip to content

Instantly share code, notes, and snippets.

Last active June 27, 2021 06:51
What would you like to do?
Delete first node
// DeleteFirst deletes the first node in a linked list.
// Deletion is not inplace. The function returns the new position of the head node after deletion.
func DeleteFirst(head *Node) *Node {
if head == nil {
fmt.Println("\nList empty")
return head
fmt.Println("\nDeleting first Node: ", *head)
// Create a new temporary pointer to point to the node being deleted.
node := head
// Head is pointing to the node. We must change head to make the node unreachable.
head = head.Next
// Make the struct store its zero value to illustrate that we are deleting the node.
// Note that doing this will not actually delete the node.
node.Next = nil
node.Data = 0
// Make the node unreachable so that the go garbage collector deletes this node.
node = nil
return head
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment