Reverse a Linked List.
Anonymous
Pretty simple, just start at the head, iterate to the next, point next at prev until you hit the end. just need to manage 3 pointers. Some Dodgy C below. void reverseList(node **n) { node *prev = *n; if(prev == NULL) return; node *cur = prev ->next; if(cur == NULL) return; prev->next = NULL; /* New End of list */ do { node *tmp = cur ->next; cur->next = prev; prev = cur; cur = tmp; } while(cur != NULL); *n = prev; /* Reset the Head */ }
Check out your Company Bowl for anonymous work chats.