Complete the code to check if the list is empty before deletion.
if head == [1] { return head }
In Go, the empty pointer is represented by nil. Checking head == nil confirms if the list is empty.
Complete the code to delete the head node if it matches the value.
if head.val == [1] { return head.next }
val or head instead of value.The function deletes the node with the given value. So, we compare head.val with value.
Fix the error in the loop condition to traverse the list correctly.
for current.next != [1] && current.next.val != value { current = current.next }
In Go, the end of the list is marked by nil. So, the loop should check current.next != nil.
Fill both blanks to correctly remove the node with the given value.
if current.next != [1] { current.next = current.next.[2] }
prev or head instead of next.current.next is nil.We check if current.next is not nil before deleting. Then, we set current.next to current.next.next to skip the node.
Fill all three blanks to complete the function that deletes a node by value.
func deleteNode(head *Node, value int) *Node {
if head == [1] {
return head
}
if head.val == [2] {
return head.next
}
current := head
for current.next != [3] && current.next.val != value {
current = current.next
}
if current.next != nil {
current.next = current.next.next
}
return head
}The function checks if the list is empty (head == nil), then if the head node has the value to delete (head.val == value), then traverses until the node before the target (current.next != nil).
