Doubly linked list reversing - printing out garbage data
<p>I've got problem with this part of code. My goal is to reverse a doubly linked list. I receive garbage values when I try to print out reversed list.</p>
<pre><code>typedef struct node{
    int val;
    struct node* prev;
    struct node* next;
}Node;

typedef struct list{
    Node* head;
    Node* tail;
}List;

void pushFront(List* l, Node* node){
    if(l-&gt;head == NULL){
        l-&gt;head = node;
        l-&gt;tail = node;
        l-&gt;tail-&gt;next = NULL;
    }else{
        l-&gt;head-&gt;prev = node;
        node-&gt;next = l-&gt;head;
        l-&gt;head = node;
    }
}
void printList(List* list){

    Node *ptr = list-&gt;head;
    while(ptr != NULL){
        printf(&quot;%i &quot;,ptr-&gt;val);
        ptr = ptr-&gt;next;
    }
    puts(&quot;&quot;);
    free(ptr);
}

void reverse(List* lista){

    Node* ptr = lista-&gt;head;
    Node* temp = NULL;
    while(ptr != NULL){
        temp = ptr-&gt;prev;
        ptr-&gt;prev = ptr-&gt;next;
        ptr-&gt;next = temp;
        ptr = ptr-&gt;prev;
    }

    if(temp != NULL)
        lista-&gt;head = temp-&gt;prev;
    free(ptr);
    free(temp);
}
</code></pre>
<p>Output I receive:</p>
<blockquote>
<p>Original list: 1 2 3 4 5 6 7</p>
<p>Reversed list: 1 8532616 3 4 5 6 7 8528368 2002618240</p>
</blockquote>