-2

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();
}
Stephen Docy
  • 4,738
  • 7
  • 18
  • 31
Bhart Kumar
  • 25
  • 2
  • 3
  • 8
  • 2
    Possible duplicate of [How to reverse a singly linked list using only two pointers?](https://stackoverflow.com/questions/1801549/how-to-reverse-a-singly-linked-list-using-only-two-pointers) – Asif Raza Jun 05 '17 at 18:30
  • What's the program output? – MrJLP Jun 05 '17 at 18:32
  • Look around more for examples on how to do this http://www.geeksforgeeks.org/write-a-function-to-reverse-the-nodes-of-a-linked-list/ – MrJLP Jun 05 '17 at 18:40
  • @A.Raza i didn't ask, how to do it by using two pointers. i wanted to know my mistake – Bhart Kumar Jun 05 '17 at 19:09

1 Answers1

1

This may help you:

void reverse(struct node** headr)
{
    struct node* current=*headr;
    struct node* prev = NULL;
    struct node* next;
    while(current!=NULL)
    {
        next=current->next;
        current->next = prev;
        prev = current;
        current = next;
    }

    *headr=prev;
}

Your Main:

int main()
{

    struct node* head = NULL;

    push(&head, 20);
    push(&head, 4);
    push(&head, 15);
    push(&head, 85);

    printf("Befor:\n");
    print(head);

    reverse(&head);

    printf("\nAfter:\n");
    print(head);

    getchar();
}

Output:

Befor:
85  15  4  20  
After:
20  4  15  85  
Asif Raza
  • 3,435
  • 2
  • 27
  • 43
  • your method worked well ,the same one i found on geeksforgeeks. but what was the mistake in my code? – Bhart Kumar Jun 05 '17 at 19:25
  • @BhartKumar Your reverse while loop was stuck bouncing between the first and second nodes (data = 85 and 15) and so never ended. Step through on a debugger to see this. if you sketch out both of these loops (yours and this answer), you should see what is wrong. – Justin J. Jun 05 '17 at 19:38
  • @Stargateur I rollback it, Sorry I posted my c++ work which I have done in course – Asif Raza Jun 05 '17 at 22:02
  • @BhartKumar Now check it I fix `while` loop issue – Asif Raza Jun 05 '17 at 22:23