Skip to content

Instantly share code, notes, and snippets.

@sivabudh
Created February 14, 2014 07:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sivabudh/8997264 to your computer and use it in GitHub Desktop.
Save sivabudh/8997264 to your computer and use it in GitHub Desktop.
The French Solution
#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;
}
@sivabudh
Copy link
Author

Screenshot of output: http://note.io/1jf033b

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment