1.09.2006

Reverse a linked list

This algorithm is a little more efficient than the last I posted about sorting a linked list, running in O(N) time. The algorithm requires a doubly linked list.


queue_length = queue_size(q);

front = q->head;
back = q->tail;

for (i = 0; i < queue_length / 2; i++) {

temp = front->e;
front->e = back->e;
back->e = temp;

front = front->next;
back = back->prev;
}

0 Comments:

Post a Comment

<< Home