Skip to content

Instantly share code, notes, and snippets.

@rajivseelam
Created September 18, 2012 12:08
Show Gist options
  • Save rajivseelam/3742784 to your computer and use it in GitHub Desktop.
Save rajivseelam/3742784 to your computer and use it in GitHub Desktop.
Reverse a Linked List
#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