#include #include #include struct node { struct node *next; int data; }; // Delete the first instance of a duplicate in the list struct node *delete_duplicate(struct node *head) { // Edge: empty list if (head == NULL || head->next == NULL) { return head; } struct node *current = head; while (current != NULL) { // 3 4 4 5 // current = 3 struct node *other = current->next; // o[ther = 4 struct node *previous = current; while (other != NULL) { if (other->data == current->data) { struct node *temp = other->next; previous->next = temp; free(other); return head; } previous = other; other = other->next; } current = current->next; } return NULL; } // DO NOT CHANGE CODE BELOW struct node *strings_to_list(int len, char *strings[]); void print_list(struct node *head); int main(int argc, char *argv[]) { // create linked list from command line arguments struct node *head = NULL; if (argc > 1) { // list has elements head = strings_to_list(argc - 1, &argv[1]); } struct node *new_head = delete_duplicate(head); print_list(new_head); return 0; } // create linked list from array of strings struct node *strings_to_list(int len, char *strings[]) { struct node *head = NULL; int i = len - 1; while (i >= 0) { struct node *n = malloc(sizeof (struct node)); assert(n != NULL); n->next = head; n->data = atoi(strings[i]); head = n; i -= 1; } return head; } // print linked list void print_list(struct node *head) { printf("["); struct node *n = head; while (n != NULL) { // If you're getting an error here, // you have returned an invalid list printf("%d", n->data); if (n->next != NULL) { printf(", "); } n = n->next; } printf("]\n"); }