Monday, May 30, 2022

Condensed List | HackerRank Certification | Remove repeated node from a Singly Linked List

 Given a list of integers, remove any nodes that have values that have previously occurred in the list and return a reference to the head of the list.   

For e.g: 

Linked List

Input : 3 --> 4 --> 3 --> 6

Output: 3 --> 4 --> 6 


public static SinglyLinkedListNode(SinglyLinkedListNode head)
{
    var hSet = new HashSet<int>();
    SinglyLinkedListNode newList = new SinglyLinkedListNode();
    while(head != null)
    {
        hSet.Add(head.data);
        head = head.next;
    }

    foreach(int i in hSet)
    {
        newList.InsertNode(i);
    }
    return (newList.head);

}

No comments:

Post a Comment

horizontal ads