Created
February 14, 2014 07:48
-
-
Save sivabudh/8997264 to your computer and use it in GitHub Desktop.
The French Solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
typedef struct node | |
{ | |
int data; | |
struct node *next; | |
}; | |
struct node* reverse(struct node* head) | |
{ | |
struct node* prev=NULL; | |
while(head != NULL) | |
{ | |
struct node* next=head->next; | |
head->next=prev; | |
prev=head; | |
head=next; | |
} | |
return prev; | |
}; | |
int main() | |
{ | |
//building the linked list | |
struct node* head=NULL; | |
struct node* n1=(struct node*)malloc(sizeof(struct node)); | |
struct node* n2=(struct node*)malloc(sizeof(struct node)); | |
struct node* n3=(struct node*)malloc(sizeof(struct node)); | |
struct node* n4=(struct node*)malloc(sizeof(struct node)); | |
struct node* temp=(struct node*)malloc(sizeof(struct node)); | |
n1->data=1; | |
n1->next=head; | |
head=n1; | |
n2->data=2; | |
n2->next=head; | |
head=n2; | |
n3->data=3; | |
n3->next=head; | |
head=n3; | |
n4->data=4; | |
n4->next=head; | |
head=n4; | |
temp=head; | |
printf("Original list\n"); | |
while(temp != NULL) | |
{ | |
printf("%i\n", temp->data); | |
temp=temp->next; | |
} | |
temp=reverse(head); | |
printf("List reversed\n"); | |
while(temp != NULL) | |
{ | |
printf("%i\n", temp->data); | |
temp=temp->next; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Screenshot of output: http://note.io/1jf033b