Posted on by Kalkicode
Code Single linked list

Remove existing duplicate node of sorted linked list in vb.net

Vb program for Remove existing duplicate node of linked list. Here more solutions.

' Include namespace system
Imports System 
'    Vb.net program for
'    Delete duplicate nodes in sorted linked list

'  Linked list node
Public Class LinkNode
    Public data As Integer
    Public [next] As LinkNode
    Public Sub New(ByVal data As Integer)
        Me.data = data
        Me.next = Nothing
    End Sub
End Class
public Class SingleLL
    Public head As LinkNode
    Public tail As LinkNode
    Public Sub New()
        Me.head = Nothing
        Me.tail = Nothing
    End Sub
    '  Add new node at the end of linked list
    Public Sub addNode(ByVal value As Integer)
        '  Create a new node
        Dim node As LinkNode = New LinkNode(value)
        if (Me.head  Is  Nothing) Then
            Me.head = node
        Else
            Me.tail.[next] = node
        End IF
        Me.tail = node
    End Sub
    '  Display linked list element
    Public Sub display()
        if (Me.head  Is  Nothing) Then
            Return
        End If
        Dim temp As LinkNode = Me.head
        '  iterating linked list elements
        while (temp IsNot Nothing)
            Console.Write(temp.data.ToString() + " → ")
            '  Visit to next node
            temp = temp.[next]
        End While
        Console.WriteLine(" NULL")
    End Sub
    '  Remove the duplicate nodes from sorted singly linked list
    Public Sub deleteDuplicate()
        if (Me.head  Is  Nothing) Then
            Return
        Else
            '  Auxiliary variables
            Dim temp As LinkNode = Me.head.[next]
            Dim current As LinkNode = Me.head
            Dim hold As LinkNode = Nothing
            '  Find and remove duplicate
            while (temp IsNot Nothing)
                '  Check duplicate node
                if (current.data = temp.data) Then
                    '   When node key are same
                    hold = temp
                Else
                    '  When node key are not same
                    current = temp
                End IF
                '  Visit to next node
                temp = temp.[next]
                if (hold IsNot Nothing) Then
                    '  Modified link value
                    current.[next] = temp
                    hold = Nothing
                Else
                    '  Change last node
                    Me.tail = current
                End IF
            End While
        End IF
    End Sub
    Public Shared Sub Main(ByVal args As String())
        Dim sll As SingleLL = New SingleLL()
        '  Sorted Linked list node
        '   1 → 1 → 2 → 3 → 4 → 4 → 4 → 5 → 6 → 7 → NULL
        sll.addNode(1)
        sll.addNode(1)
        sll.addNode(2)
        sll.addNode(3)
        sll.addNode(4)
        sll.addNode(4)
        sll.addNode(4)
        sll.addNode(5)
        sll.addNode(6)
        sll.addNode(7)
        Console.WriteLine(" Before Delete")
        sll.display()
        sll.deleteDuplicate()
        Console.WriteLine(" After Delete")
        '  1 → 2 → 3 → 4 → 5 → 6 → 7 → NULL
        sll.display()
    End Sub
End Class

Output

 Before Delete
1 → 1 → 2 → 3 → 4 → 4 → 4 → 5 → 6 → 7 →  NULL
 After Delete
1 → 2 → 3 → 4 → 5 → 6 → 7 →  NULL

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment