Skip to content

Commit

Permalink
Remove left side bigger value node
Browse files Browse the repository at this point in the history
  • Loading branch information
Han1018 committed Feb 28, 2024
1 parent c96eaca commit d7c68a7
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,27 @@ void q_sort(struct list_head *head, bool descend)
* the right side of it */
int q_ascend(struct list_head *head)
{
// https://leetcode.com/problems/remove-nodes-from-linked-list/
struct list_head *last_biggest, *tmp, *li;
if (list_empty(head) || list_is_singular(head))
return 0;

element_t *last_biggest_entry = list_entry(head->next, element_t, list);
last_biggest = head->next;

list_for_each_safe (li, tmp, head) {
element_t *entry = list_entry(tmp, element_t, list);
if (li == head->prev) {
return 0;
}
if (strcmp(entry->value, last_biggest_entry->value) > 0) {
while (last_biggest != tmp) {
struct list_head *next = last_biggest->next;
list_del(last_biggest);
last_biggest = next;
}
}
}
return 0;
}

Expand Down

0 comments on commit d7c68a7

Please sign in to comment.