2025 CSCI2100E Lab Session #1

Remove Duplicates from Sorted Linked List 2025 (Required)

Time Limit: 500ms
Memory Limit: 256mb

Description:
Given an unsorted linked list, delete all duplicates and keep the first-occurring elements such that each element appears only once. The linked list is not sorted in any particular order.


Input Format:
• The first line contains an integer n, representing the number of elements in the linked list.
• The second line contains n integers, representing the elements of the linked list.
Output Format:
• Print the linked list after removing duplicates, with each element separated by " -> ". End with "NULL".

Sample Input1: 
6
1 3 2 1 4 2
Sample Output1: 
1->3->2->4->NULL
Sample Input2: 
3
1 1 1
Sample Output2: 
1->NULL


Code Template:
#include <stdio.h>
#include <stdlib.h>

typedef struct linked_list_node {
    int value;
    struct linked_list_node* next;
} Node;

typedef Node* List;

List Insert(List head, int value) {
    Node* ptr = (List)malloc(sizeof(Node));
    ptr->value = value;
    ptr->next = NULL;

    if (head == NULL) {
        return ptr;
    }

    Node* current = head;
    while (current->next != NULL) {
        current = current->next;
    }
    current->next = ptr;
    return head;
}

void Print(List head) {
    while (head != NULL) {
        printf("%d", head->value);
        if (head->next != NULL) {
            printf("->");
        }
        head = head->next;
    }
    printf("->NULL\n");
}

List RemoveDuplicates(List head) {
    if (head == NULL) {
        return head;
    }

    Node* current = head;
    // Implement your code here


    return head;
}

int main() {
    List head = NULL;
    int n, value;

    // Input the number of elements in the linked list
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &value);
        head = Insert(head, value);
    }

    // Remove duplicates
    head = RemoveDuplicates(head);

    // Print the result
    Print(head);

    return 0;
}


Submit