Golang program to delete duplicate value nodes from a sorted linked list


In this Golang article, we are going to delete duplicate value nodes from a sorted linked list by using recursion and iterative method.

A linked list is a data structure consisting of a collection of nodes, where each node contains a value and a pointer to the next node in the list.

Syntax

func deleteDuplicates(head *Node) *Node{…}

The deleteDuplicates() function is used to delete duplicate value nodes from a sorted linked list. It takes pointer to the head node as its argument.

Algorithm

  • Step 1 − First, we need to import the fmt package.

  • Step 2 − Now, create a struct of a single node of the linked list named, Node. It contains two members, one to hold data value of node and second is the pointer to point to the next node in the list.

  • Step 3 − Define the function, insert() to insert the nodes in the linked list starting from the head node.

  • Step 4 − Now, create a function named deleteDuplicates that takes the head of the linked list as input and returns the head of the updated list. It uses an iterative approach to traverse the list and delete the duplicate nodes.

  • Step 5 − Initialize the current pointer to point the head of the list. And list is traversed using loop until the current becomes nil.

  • Step 6 − If the data value of the current node is equal to its next node, then delete the next node by assigning the next pointer to next to the next node.

  • Step 7 − If the data value of the current node is not equal to its next node, then move to the next node by updating the current pointer to its next field.

  • Step 8 − Return the head of the updated list.

  • Step 9 − Start the main() function. Inside the main() function, call insert() function and add nodes to the linked list.

  • Step 10 − Now, call the deleteDuplicates() function to remove the duplicates and prints the updated list.

  • Step 11 − Further, the resultant updated linked list without duplicates is printed on the screen by using the fmt.Println() function.

Example 1

In this example, we will define a deleteDuplicates() function using iterative method that is used to delete duplicate value nodes from a sorted linked list.

package main

import "fmt"

type Node struct {
   value int
   next  *Node
}

func insert(head **Node, value int) {
   newNode := &Node{value: value}
   if *head == nil || (*head).value >= value {
      newNode.next = *head
      *head = newNode
   } else {
      current := *head
      for current.next != nil && current.next.value < value {
         current = current.next
      }
      newNode.next = current.next
      current.next = newNode
   }
}

func deleteDuplicates(head *Node) *Node {
   if head == nil {
      return head
   }
   current := head
   for current.next != nil {
      if current.value == current.next.value {
         current.next = current.next.next
      } else {
         current = current.next
      }
   }
   return head
}

func printList(head *Node) {
   for head != nil {
      fmt.Printf("%d ->", head.value)
      head = head.next
   }
   fmt.Println("nil")
}

func main() {
   var head *Node
   insert(&head, 4)
   insert(&head, 3)
   insert(&head, 1)
   insert(&head, 2)
   insert(&head, 3)
   insert(&head, 4)

   fmt.Println("Sorted Linked List:")
   printList(head)

   deleteDuplicates(head)

   fmt.Println("List after deleting duplicate Value Nodes:")
   printList(head)
}

Output

Sorted Linked List:
1 -> 2 -> 3 -> 3 -> 4 -> 4 -> nil
List after deleting duplicate Value Nodes:
1 -> 2 -> 3 -> 4 -> nil 

Example 2

In this example, we will define a deleteDuplicates() function using recursive method that is used to delete duplicate value nodes from a sorted linked list.

package main

import (
   "fmt"
)

type Node struct {
   data int
   next *Node
}

func deleteDuplicates(head *Node) *Node {
   if head == nil || head.next == nil {
      return head
   }
   head.next = deleteDuplicates(head.next)
   if head.data == head.next.data {
      return head.next
   }
   return head
}

func insert(head **Node, data int) {
   newNode := &Node{data: data, next: *head}
   *head = newNode
}

func printList(head *Node) {
   for head != nil {
      fmt.Printf("%d ->", head.data)
      head = head.next
   }
   fmt.Println("nil")
}

func main() {
   var head *Node = nil

   insert(&head, 6)
   insert(&head, 6)
   insert(&head, 4)
   insert(&head, 3)
   insert(&head, 2)
   insert(&head, 2)
   insert(&head, 1)

   fmt.Println("Sorted Linked List:")
   printList(head)

   head = deleteDuplicates(head)

   fmt.Println("Linked List after deleting duplicate Value Nodes:")
   printList(head)
}

Output

Sorted Linked List:
1 -> 2 -> 2 -> 3 -> 4 -> 6 -> 6 -> nil
Linked List after deleting duplicates:
1 -> 2 -> 3 -> 4 -> 6 -> nil 

Conclusion

We have successfully compiled and executed a go language program to delete duplicate value nodes from a sorted linked list by using recursion and iterative methodalong with two examples. In the first example, we have used the iterative method and in the second example, we have used the recursive method.

Updated on: 10-May-2023

188 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements