This brilliant code snippet takes a node offset by the index from the head and swaps its value with the node offset by the index from the tail.
void swapAt(Node *head, int index) { Node * node = head; Node * begin, trailer; for (int i = 0; node != NULL; node = node->next, ++i ) { if ( i == index ) { begin = node; trailer = head; } if ( i > index) trailer = trailer->next; } int temp = begin.val; begin.val = trailer.val; trailer.val = temp; }
Комментариев нет:
Отправить комментарий