Skip to content

Commit

Permalink
Implement q_reverse() q_reverseK() function
Browse files Browse the repository at this point in the history
  • Loading branch information
ssheep773 committed Mar 2, 2024
1 parent ac1a918 commit 1ec2307
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,36 @@ void q_swap(struct list_head *head)
}

/* Reverse elements in queue */
void q_reverse(struct list_head *head) {}
void q_reverse(struct list_head *head)
{
if (!head)
return;
struct list_head *cur, *safe;
list_for_each_safe (cur, safe, head) {
list_move(cur, head);
}
}

/* Reverse the nodes of the list k at a time */
void q_reverseK(struct list_head *head, int k)
{
// https://leetcode.com/problems/reverse-nodes-in-k-group/
// https://leetcode.com/problems/reverse-nodes-in-k-group/ komark06
if (!head || list_empty(head))
return;
struct list_head *node, *safe, *cut = head;
LIST_HEAD(tmp);
int count = 0;
list_for_each_safe (node, safe, head) {
count++;
if (count == k) {
list_cut_position(&tmp, cut, node);
q_reverse(&tmp);
count = 0;
list_splice(&tmp, cut); //

cut = safe->prev;
}
}
}

/* Sort elements of queue in ascending/descending order */
Expand Down

0 comments on commit 1ec2307

Please sign in to comment.