Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions gfg/gfgAnk.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
//Initial Template for C
#include<stdio.h>
#include<stdlib.h>

struct Node
{
int data;
struct Node *next;

}*start;

void insert();
void display(struct Node *head);

// } Driver Code Ends
//User function Template for C



struct Node* reverseList(struct Node *head)
{
struct Node* current = head;
struct Node* prev = NULL;
struct Node* nextptr = NULL;

while(current != NULL){
nextptr = current->next;
current->next = prev;
prev = current;
current = nextptr;
}

return prev;

// if(head->next == NULL){
// return head;
// }

// struct Node* newHead = reverseList(head->next);
// head->next->next = head;
// head->next = NULL;
// return newHead;
}

// { Driver Code Starts.

int main()
{
int t;
scanf("%d",&t);
while(t--)
{
start=NULL;
insert();
start = reverseList(start);
display(start);
printf("\n");
}
return 0;

}


void insert()
{
int n,value,i;
scanf("%d",&n);
struct Node *temp;
for(i=0;i<n;i++)
{
scanf("%d",&value);
if(i==0)
{
start=(struct Node *) malloc( sizeof(struct Node) );
start->data=value;
start->next=NULL;
temp=start;
continue;
}
else
{
temp->next= (struct Node *) malloc( sizeof(struct Node) );
temp=temp->next;
temp->data=value;
temp->next=NULL;
}
}
}

void display(struct Node *head)
{
while(head!=NULL)
{
printf("%d ",head->data);
head=head->next;
}
}




// } Driver Code Ends