Skip to content

Commit a56881b

Browse files
committed
Implement q_delete_dup
Implement q_delete_dup Previously, q_delete_dup was a placeholder that only returned true without performing any deletion. This commit implements the function to properly remove duplicate elements from a list. The function traverses the list, detecting and deleting all nodes with duplicate values to ensure uniqueness. Change-Id: I395f4127532880664eb305288cf7835e892fde05
1 parent cba4f00 commit a56881b

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

queue.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,29 @@ bool q_delete_mid(struct list_head *head)
128128
bool q_delete_dup(struct list_head *head)
129129
{
130130
// https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/
131+
if (!head || list_empty(head))
132+
return false;
133+
struct list_head *cur = head->next, *next, *node;
134+
while (cur != head) {
135+
int duplicated = 0;
136+
next = node = cur->next;
137+
while (node != head) {
138+
struct list_head *temp = node->next;
139+
if (strcmp(list_entry(cur, element_t, list)->value,
140+
list_entry(node, element_t, list)->value) == 0) {
141+
duplicated = 1;
142+
list_del(node);
143+
q_release_element(list_entry(node, element_t, list));
144+
}
145+
node = temp;
146+
}
147+
if (duplicated) {
148+
next = cur->next;
149+
list_del(cur);
150+
q_release_element(list_entry(cur, element_t, list));
151+
}
152+
cur = next;
153+
}
131154
return true;
132155
}
133156

0 commit comments

Comments
 (0)