I implemented reversing a linked list in iterative way, just by knowing we need 3 pointers and I have never seen a code before, when I coded. I ran this code and tested it, it works fine. Even for null element and one element. But all the implementations on net have slightly different code. I was just wondering, how my algorithm is.
Do you guys see any major concern in my implementation?
int reverse(List *l) {
if(!l) {
printf("List is NULL \n");
return -1;
}
if(!l->head) {
printf("List is empty \n");
return -1;
}
Node *prev_node = l->head;
Node *curr_node = l->head->next;
while(curr_node) {
prev_node->next = curr_node->next;
curr_node->next = l->head;
l->head = curr_node;
curr_node = prev_node->next;
}
return 0;
}