Created
September 18, 2012 12:08
-
-
Save rajivseelam/3742784 to your computer and use it in GitHub Desktop.
Reverse a Linked List
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> | |
#include<stdlib.h> | |
typedef struct _node{ | |
int value; | |
struct _node * next; | |
} node; | |
typedef node linkedlist; | |
linkedlist * Insert(int X, linkedlist * L){ | |
node * temp; | |
temp = (node *)malloc(sizeof(node)); | |
temp->value = X; | |
temp->next = NULL; | |
if(L == NULL) | |
return temp; | |
else{ | |
temp->next = L; | |
return temp; | |
} | |
} | |
void printLL(linkedlist * L){ | |
node * temp = L; | |
printf("List : "); | |
while(temp != NULL){ | |
printf("%d ", temp->value); | |
temp = temp->next; | |
} | |
printf("\n"); | |
} | |
linkedlist * RevLL(linkedlist * L){ | |
node *previous, *present, *next; | |
previous = NULL; | |
present = L; | |
next = L->next; | |
while(present!=NULL){ | |
present->next = previous; | |
previous = present; | |
present = next; | |
if(next!=NULL) | |
next = next->next; | |
} | |
printf("Reversed..!\n"); | |
return previous; | |
} | |
int main(){ | |
linkedlist * L; | |
int i; | |
for(i=0;i<10;i++) | |
L = Insert(i, L); | |
printLL(L); | |
L = RevLL(L); | |
printLL(L); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment