Skip to content

Commit

Permalink
Implement q_delete_dup function:
Browse files Browse the repository at this point in the history
Implement q_delete_dup() to delete all nodes containing duplicate strings.
  • Loading branch information
aftuta85 committed Mar 21, 2024
1 parent a544d2d commit bc4802d
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,51 @@ bool q_delete_mid(struct list_head *head)
return true;
}

static void q_delete_dup_free_helper(struct list_head *del)
{
if (!del)
return;

element_t *del_e = list_entry(del, element_t, list);
list_del_init(&del_e->list);
free(del_e->value);
free(del_e);
}

/* Delete all nodes that have duplicate string */
bool q_delete_dup(struct list_head *head)
{
// https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/
if (!head)
return false;

// Need to sort the list first
q_sort(head, 0);

struct list_head **indir = &head->next;
struct list_head *del = NULL;
element_t *e, *next_e;
while (*indir != head && (*indir)->next != head) {
e = list_entry(*indir, element_t, list);
next_e = list_entry((*indir)->next, element_t, list);
if (!strcmp(e->value, next_e->value)) {
while (*indir != head && (*indir)->next != head) {
e = list_entry(*indir, element_t, list);
next_e = list_entry((*indir)->next, element_t, list);
if (!strcmp(e->value, next_e->value)) {
del = *indir;
*indir = (*indir)->next;
q_delete_dup_free_helper(del);
del = *indir;
} else {
*indir = (*indir)->next;
break;
}
}
q_delete_dup_free_helper(del);
del = NULL;
} else
indir = &(*indir)->next;
}
return true;
}

Expand Down

0 comments on commit bc4802d

Please sign in to comment.