i have made code for reversing the linked list. if i run this code given linked list is printed but not reversed one.
I think there is some mistake in reverse function.
can someone pls.. tell me the mistake in my code. i have done it using three pointers
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node* next;
};
void reverse(struct node** headr) {
struct node* current=*headr;
struct node*temp=*headr;
struct node* prev;
struct node* next;
while (current!=NULL) {
prev=current;
current=current->next;
next=current;
next->next=prev;
}
temp->next=NULL;
*headr=current;
}
void push(struct node** headr, int new_data) {
struct node* new_node = (struct node*) malloc(sizeof(struct node));
new_node->data = new_data;
new_node->next = (*headr);
(*headr) = new_node;
}
void print(struct node *head) {
struct node *temp = head;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
}
int main() {
struct node* head = NULL;
push(&head, 20);
push(&head, 4);
push(&head, 15);
push(&head, 85);
printf("Given linked list\n");
print(head);
reverse(&head);
printf("\nReversed Linked list \n");
print(head);
getchar();
}