queue.c
/*
 * Attempt to swap every two adjacent nodes.
 */
Attempt to swap every two adjacent nodes.
Attempt to swap every two adjacent nodes .
void q_swap(struct list_head *head)
{
    if (!head || head->next == head || head->next->next == head)
        return;
    for (struct list_head *node1 = head->next, *node2 = head->next->next;
         node1 != head && node2 != head;
         node1 = node1->next, node2 = node1->next) {
        node1->prev->next = node2;
        node2->prev = node1->prev;
        node1->prev = node2;
        node1->next = node2->next;
        node2->next->prev = node1;
        node2->next = node1;
    }
}
void q_swap ( struct list_head * head ) { if ( ! head || head -> next == head || head -> next -> next == head ) return ; for ( struct list_head * node1 = head -> next , * node2 = head -> next -> next ; node1 != head && node2 != head ; node1 = node1 -> next , node2 = node1 -> next ) { node1 -> prev -> next = node2 ; node2 -> prev = node1 -> prev ; node1 -> prev = node2 ; node1 -> next = node2 -> next ; node2 -> next -> prev = node1 ; node2 -> next = node1 ; } }
Attempt to swap every two adjacent nodes.
Attempt to swap every two adjacent nodes .
// https://leetcode.com/problems/swap-nodes-in-pairs/