-
-
Save AahanSingh/29587d73561ba77cd57004d9341f465c to your computer and use it in GitHub Desktop.
Delete first node
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
// 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