Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ReverseLL #162

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
60 changes: 60 additions & 0 deletions C++/ReverseLL.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include<bits/stdc++.h>
using namespace std;

struct Node{
int data;
struct Node *next;
}*root=NULL;

//Program to reverse a linked list

int main()
{
int n,x,i;
struct Node *t,*p=NULL,*r;
cout<<"Enter the number of elements:\n";
cin>>n;
cout<<"Enter the elements\n";

for(i=0;i<n;i++)
{
cin>>x;
t = new Node;
t->data = x;
if(root == NULL)
root = t;
t->next = NULL;
if(p!=NULL)
p->next=t;
p = t;
}

t = root;
cout<<"Elements before reversing are : ";
while(t!=NULL)
{
cout<<t->data<<" ";
t=t->next;
}
t = root;
while(t!=NULL)
{
r = new Node;
r->data = t ->data;
if(t==root)
r->next = NULL;
else
r->next = p;
t = t->next;
p=r;
}

cout<<"After reversing: ";
while(p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}

return 0;
}