Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gallirohik/e10500283b624f73ecc2e3877280c4b1 to your computer and use it in GitHub Desktop.
Save gallirohik/e10500283b624f73ecc2e3877280c4b1 to your computer and use it in GitHub Desktop.
Reverse of a linked list without recursion created by Rohik - https://repl.it/@Rohik/Reverse-of-a-linked-list-without-recursion
#include <stdio.h>
#include <stdlib.h>
struct tnode
{
int data;
struct tnode *link;
};
typedef struct tnode *tptr;
tptr insert(tptr first,int x)
{
tptr p,t;
p=(tptr)malloc(sizeof(struct tnode));
p->data=x;
p->link=NULL;
if(first==NULL)
{
first=p;
return first;
}
t=first;
while(t->link!=NULL)
t=t->link;
t->link=p;
return first;
}
void display(tptr first)
{
tptr t;
t=first;
while(t!=NULL)
{
printf("%d ",t->data);
t=t->link;
}
}
tptr reverse(tptr head)
{
tptr prev=NULL,next=NULL,current=NULL;
current=head;
while(current!=NULL)
{
next=current->link;
current->link=prev;
prev=current;
current=next;
}
return prev;
}
int main()
{
int x;
tptr first;
first=NULL;
scanf("%d",&x);
while(x!=-1)
{
first=insert(first,x);
scanf("%d",&x);
}
first=reverse(first);
display(first);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment