Skip to content

Instantly share code, notes, and snippets.

@MattDiesel
Last active April 26, 2017 01:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MattDiesel/1eb6167304f5e23b98c1fa9cd6346736 to your computer and use it in GitHub Desktop.
Save MattDiesel/1eb6167304f5e23b98c1fa9cd6346736 to your computer and use it in GitHub Desktop.
Delete multiples for prime sieve on linked list
node* Deletemultiples(node* head, int k)
{
node* temp = head, *old = head, *todelete;
if (head == NULL)
return NULL;
while (temp!=NULL) {
if (temp->data % k ==0 && temp->data != k) {
todelete = temp;
if (temp == head)
head = temp->next;
else
old->next = temp->next;
temp = temp->next;
free(todelete);
}
else {
old = temp;
temp = temp->next;
}
}
return head;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment