> For the complete documentation index, see [llms.txt](https://nitinranganath.gitbook.io/data-structures/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://nitinranganath.gitbook.io/data-structures/doubly-linked-list/reversing-a-doubly-linked-list.md).

# Reversing a Doubly Linked List

```c
void reverse(struct node *ptr) {

    // Temporary pointer for swapping
    struct node *temp;
    
    while (ptr != NULL) {
        // Swapping the links
        temp = ptr -> next;
        ptr -> next = ptr -> prev;
        ptr -> prev = temp;
        // Moving forward
        ptr = ptr -> prev;
        // Condition to set head node
        if (ptr != NULL && ptr -> next == NULL) {
            head = ptr;
        }
    }
    
}
```

Contributed by Nitin Ranganath
