Skip to content

Commit 681806a

Browse files
committed
Refactor q_reverse to use list_move
Refactor this function by replacing manual pointer swapping with list_move(), making the code more concise and conforming to the Linux kernel style. Change-Id: Idb6d82d19f35928aa379eb3bdced5d6a09a1b6f8
1 parent c37bc7f commit 681806a

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

queue.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -165,21 +165,17 @@ void q_reverse(struct list_head *head)
165165
{
166166
if (!head || list_empty(head))
167167
return;
168-
struct list_head *curr, *safe, *temp;
169-
list_for_each_safe (curr, safe, head) {
170-
temp = curr->next;
171-
curr->next = curr->prev;
172-
curr->prev = temp;
173-
}
174-
temp = head->next;
175-
head->next = head->prev;
176-
head->prev = temp;
168+
struct list_head *curr, *safe;
169+
list_for_each_safe (curr, safe, head)
170+
list_move(curr, head);
177171
}
178172

179173
/* Reverse the nodes of the list k at a time */
180174
void q_reverseK(struct list_head *head, int k)
181175
{
182176
// https://leetcode.com/problems/reverse-nodes-in-k-group/
177+
if (!head || list_empty(head))
178+
return;
183179
}
184180

185181
/* Sort elements of queue in ascending/descending order */

0 commit comments

Comments
 (0)