Write a function that searches a linked list for the integers that occur more than once, and removes all their occurrences except for the first occurrences. The function does not remove the integers that occur only once. Removed nodes must be freed. Each node is defined asstruct node{ int data; struct node * next;};The function is declared as follows. Its input is the head pointer of the linked list. It returns the head pointer of the linked list after the removal.struct node *remove_repeated_integer(struct node *list);
Question
Write a function that searches a linked list for the integers that occur more than once, and removes all their occurrences except for the first occurrences. The function does not remove the integers that occur only once. Removed nodes must be freed. Each node is defined asstruct node{ int data; struct node * next;};The function is declared as follows. Its input is the head pointer of the linked list. It returns the head pointer of the linked list after the removal.struct node *remove_repeated_integer(struct node *list);
Solution
Here is a C function that implements the desired functionality:
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *remove_repeated_integer(struct node *list) {
struct node *current = list;
struct node *temp = NULL;
struct node *prev = NULL;
while (current != NULL) {
temp = current->next;
prev = current;
while (temp != NULL) {
if (current->data == temp->data) {
prev->next = temp->next;
free(temp);
temp = prev->next;
} else {
prev = temp;
temp = temp->next;
}
}
current = current->next;
}
return list;
}
This function works by iterating over the linked list with two pointers, current and temp. For each node in the list, it checks all the following nodes for duplicates. If a duplicate is found, it is removed from the list and its memory is freed. The function returns the head of the modified list.
Similar Questions
Given a sorted linked list, delete all nodes that have duplicate numbers (all occurrences), leaving only numbers that appear once in the original list, and return the head of the modified linked list. Examples:Input: Linked List = 23->28->28->35->49->49->53->53Output: 23 35Explanation: The duplicate numbers are 28, 49 and 53 which are removed from the list.Input: Linked List = 11->11->11->11->75->75Output: Empty listExplanation: All the nodes in the linked list have duplicates. Hence the resultant list would be empty.
You are given a sequence of integers terminated with a -1. The -1 isnot part of the input sequence. Next, you are given a positive number N. You have to create a linked list with the input sequence of integersas entries. You can use the following structure. struct node{ int data;struct node *next; }; Now, you have to delete all but the last N elements from the linkedlist, and print the resulting list. (i.e. The resulting list willconsist of only the last N elements from the list.) If N is longer than the length of the linked list, you must print -1. While printing, the entries of the list must be separated by a singlespace. Sample Input3 4 5 6 -11 Sample Output6
Given pointer to a node X in a singly linked list. Only one pointer is given, pointer to head node is not given, can we delete the node X from given linked list?Possible if X is not last node. Use following two steps (a) Copy the data of next of X to X. (b) Delete next of X.Possible if size of linked list is even.Possible if size of linked list is oddPossible if X is not first node. Use following two steps (a) Copy the data of next of X to X.
Given the head of a linked list, we repeatedly delete consecutive sequences of nodes that sum to 0 until there are no such sequences.After doing so, return the head of the final linked list. You may return any such answer.
Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well. Example 1:Input: head = [1,1,2]Output: [1,2]Example 2:Input: head = [1,1,2,3,3]Output: [1,2,3] Constraints:The number of nodes in the list is in the range [0, 300].-100 <= Node.val <= 100The list is guaranteed to be sorted in ascending order.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.