Day 24: More Linked List - 30 Days Code - IndianTechnoEra
Latest update Android YouTube

Day 24: More Linked List - 30 Days Code

Introduction:

Welcome to Day 24 of the 30 Days of Code challenge! In today's challenge, we'll be working with linked lists. If you haven't already, check out the Tutorial tab for valuable learning materials and an instructional video.


The Challenge:

In today's task, you are provided with a Node class that represents a node in a linked list. Each node has an integer data field and a Node instance pointer pointing to the next node in the list. Your goal is to complete the removeDuplicates function. This function should take a pointer to the head node of a linked list, remove any duplicate nodes, and return the head of the updated list.


Format & Sample:

The input consists of the following:


The first line contains an integer, T, the number of nodes to be inserted.

The T subsequent lines each contain an integer describing the value of a node being inserted at the list's tail.

The output should be the head of the updated linked list. The provided code includes a function to display the linked list.


Input Format:

6

1

2

2

3

3

4


Output Format:

1 2 3 4


Explanation:

The initial list is [1, 2, 2, 3, 3, 4]. Duplicate values 2 and 3 occur twice in the list, so the removeDuplicates function should remove the duplicate nodes. The updated list is [1, 2, 3, 4].


Code Breakdown:

Reading Input:

int main() {

    Node* head = NULL;

    Solution mylist;

    int T, data;

    cin >> T;

    while (T-- > 0) {

        cin >> data;

        head = mylist.insert(head, data);

    }

    head = mylist.removeDuplicates(head);


    mylist.display(head);

}


Removing Duplicates:

Node* removeDuplicates(Node* head) {

    if (head == NULL || head->next == NULL) {

        return head;

    }


    Node* current = head;

    while (current->next != NULL) {

        if (current->data == current->next->data) {

            Node* temp = current->next;

            current->next = current->next->next;

            delete temp;

        } else {

            current = current->next;

        }

    }

    return head;

}


Final Code:

#include <cstddef>
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

class Node {
public:
    int data;
    Node* next;
    Node(int d) {
        data = d;
        next = NULL;
    }
};

class Solution {
public:
    Node* removeDuplicates(Node* head) {
        if (head == NULL || head->next == NULL) {
            return head;
        }

        Node* current = head;
        while (current->next != NULL) {
            if (current->data == current->next->data) {
                Node* temp = current->next;
                current->next = current->next->next;
                delete temp;
            } else {
                current = current->next;
            }
        }
        return head;
    }

    Node* insert(Node* head, int data) {
        Node* p = new Node(data);
        if (head == NULL) {
            head = p;
        } else if (head->next == NULL) {
            head->next = p;
        } else {
            Node* start = head;
            while (start->next != NULL) {
                start = start->next;
            }
            start->next = p;
        }
        return head;
    }

    void display(Node* head) {
        Node* start = head;
        while (start) {
            cout << start->data << " ";
            start = start->next;
        }
    }
};

int main() {
    Node* head = NULL;
    Solution mylist;
    int T, data;
    cin >> T;
    while (T-- > 0) {
        cin >> data;
        head = mylist.insert(head, data);
    }
    head = mylist.removeDuplicates(head);

    mylist.display(head);
    return 0;
}


This concludes Day 24 of the 30 Days of Code challenge. You've successfully implemented the removal of duplicate nodes from a linked list. Continue exploring and coding!

Post a Comment

Feel free to ask your query...
Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.