Golang program to remove elements from the linked list


In Go, a linked list is a linear data structure with pointers connecting the items, and from the first node (head) to the last node, each node can be visited (tail). We will execute the program of removing elements from the linked list using two examples. The first example uses node struct whereas the second example uses a dummy node

Method 1: Using Node Struct

This code creates a Node struct that has two fields: Value and Next, which links to the next node in the list. The remove_node method removes the node with the supplied value from the list and returns the new head of the list. It accepts the list's head and the value to be removed as arguments.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) package in the program where main produces executable codes and fmt helps in formatting input and output.

  • Step 2 − Create a node struct with a value of type int and next of type node.

  • Step 3 − Create a function remove_node and in that function verify that the head node is null. If it is, give nil back.

  • Step 4 − Verify that the value of the head node matches the value that will be removed. Return the following node in the list if it is.

  • Step 5 − In the next step, set the head node as the current node's pointer.

  • Step 6 − Then, traverse the list as long as the following node is not zero.

  • Step 7 − Update the current node's next pointer to skip the following node and end the loop if the value of the following node is equal to the value to be eliminated.

  • Step 8 − Place the next node as the current node's pointer.

  • Step 9 − Return the head node to the following function.

  • Step 10 − In the main function print the current value after removing elements from the linked list.

Example

In this example we will use node struct to remove elements from the linked list.

package main
import "fmt"

type Node struct {
   Value int
   Next  *Node
}

func remove_node(head *Node, value int) *Node {
   if head == nil {
      return nil
   }

   if head.Value == value {
      return head.Next
   }

   current := head //remove the elements from the list
   for current.Next != nil {
      if current.Next.Value == value {
         current.Next = current.Next.Next
         break
      }
      current = current.Next
   }
   
   return head
}

func main() {
   head := &Node{Value: 1, Next: &Node{Value: 2, Next: &Node{Value: 3, Next: nil}}}
   fmt.Println("The linked list after removal of element is:")
   head = remove_node(head, 2)
   for current := head; current != nil; current = current.Next {
      fmt.Println(current.Value) //print the modified linked list
   }
}

Output

The linked list after removal of element is:
1
3

Method 2: Using Dummy Node

This method employs a dummy node to make the edge cases simpler. The remove_node function removes the node with the supplied value by taking the list's head and the value to be removed as arguments and returning the list's new head. Let’s see through the code and algorithm to understand the concept.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) package in the program where main produces executable codes and fmt helps in formatting input and output.

  • Step 2 − Create a Node struct with num_value of type int and Next of type Node.

  • Step 3 − Create a function remove_node with two inputs head and value. Then, make a dummy node and set its next field to the list's head.

  • Step 4 − Create a prior node pointer that points to the dummy node.

  • Step 5 − In the next step, traverse the list as long as the following node is not zero.

  • Step 6 − Update the previous node's next pointer to skip the subsequent node and end the loop if the value of the subsequent node is identical to the value to be deleted.

  • Step 7 − Then, to the following node, move the previous node pointer.

  • Step 8 − The head of the list following the removal of the node with the provided value serves as the next field for the dummy node.

  • Step 9 − Return the dummy.next to the function and in the main function print the linked list using fmt.Println() function where ln means new line.

Example

In this example, we will use dummy node to remove elements from linked list.

package main
import "fmt"

type Node struct {
   num_value int
   Next      *Node
}

func remove_node(head *Node, value int) *Node {
   dummy := &Node{Next: head} //remove the required element from the list
   prev := dummy
   for prev.Next != nil {
      if prev.Next.num_value == value {
         prev.Next = prev.Next.Next
         break
      }
      prev = prev.Next
   }
   return dummy.Next
}
func main() {
   fmt.Println("The linked list after removal of elements:")
   head := &Node{num_value: 1, Next: &Node{num_value: 2, Next: &Node{num_value: 3, Next: nil}}}
   head = remove_node(head, 2)
   for current := head; current != nil; current = current.Next {
      fmt.Println(current.num_value)   //print the modified list
   }
}

Output

The linked list after removal of elements:
1
3

Conclusion

We executed the program of removing elements from the linked list using two examples. In the first example, we used node struct and in the second example, we will use the dummy node to execute the programs.

Updated on: 20-Feb-2023

512 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements