Reverse a doubly linked list
Anonymous
struct Node { int data; struct Node* next; struct Node* prev; } void Reverse(struct Node **head) { if(*head == NULL) { printf("List is empty"); return NULL; } struct Node* current = *head; struct Node* temp = NULL; while(current != NULL) { //swapping the current and next pointers of doubly linked list temp = current->prev; current->prev = current->next; current->next = temp; //moving to new node current = current->prev; } //changing the head pointer and making sure that the list was not empty or with just one element if(temp!= NULL) *head = temp->prev; }
Check out your Company Bowl for anonymous work chats.