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

Debugged old reversing singly linked list code and added a new code for the same too. #6755

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
82 changes: 82 additions & 0 deletions code/data_structures/src/Linked_List/Reversing_singly_LL.cpp
@@ -0,0 +1,82 @@
#include "bits/stdc++.h"
using namespace std;

//creating a class Node for Linked List
class Node
{
public:
int data;
Node* next;

//constructor declaration
Node(int data)
{
this->data=data;
this->next=NULL;
}
};

//Taking reference by call to change the original Linked List and reverse it

void reverseList(Node* &head)
{
Node* prev = NULL;
Node* forw = NULL;
Node* curr = head;

while(curr != NULL)
{
forw = curr -> next;
curr -> next = prev;
prev = curr;
curr = forw;
}
head=prev;
}

//insertion of a node at the tail of the Linked list
void insertAtTail(Node* &tail, int data)
{
Node *temp =new Node(data);
tail->next=temp;
tail=temp;

}

//printing out the linked list
void print(Node * &head)
{
Node * temp=head;

while(temp!=NULL)
{
cout<<temp->data<<" ";
temp =temp->next;
}
cout<<endl;
}

int main()
{
Node * node1=new Node(0);

Node *head =node1;
Node* tail=node1;

insertAtTail(tail,1);
insertAtTail(tail,2);
insertAtTail(tail,3);
insertAtTail(tail,4);
insertAtTail(tail,5);
insertAtTail(tail,6);

cout<<"Before reversing: ";
print(head);

reverseList(head);

cout<<"After reversing: ";
print(head);

return 0;
}
111 changes: 65 additions & 46 deletions code/data_structures/src/Linked_List/reversing_linkedlist.cpp
@@ -1,56 +1,75 @@
#include<bits/stdc++.h>
using namespace std;

struct Node{
int value;
struct Node* next;
Node(int value){
this->value=value;
next=NULL;
}

int value;
struct Node* next;

Node(int value)
{
this->value=value;
next=NULL;
}
};

struct LL{
Node* head;
LL(){
head=NULL;
}
void reverse(){
Node* curr=head;
Node* prev=NULL,*next=NULL;
while(curr!=NULL){
next=curr->next;
curr->next=prev;
prev=current;
current=next;
}
head=prev;
}
void print(){
struct Node* temp=head;
while(temp!=NULL){
cout<<temp->value<<" ";
temp=temp->next;
}
}
void push(int value){
Node* temp=new Node(value);
temp->next=head;
head=temp;
}
Node* head;
LL()
{
head=NULL;
}

void reverse()
{
Node* curr=head;
Node* prev=NULL,*next=NULL;

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

head=prev;
}

void print()
{
struct Node* temp=head;

while(temp!=NULL)
{
cout<<temp->value<<" ";
temp=temp->next;
}
}

void push(int value)
{
Node* temp=new Node(value);
temp->next=head;
head=temp;
}
};

int main(){
LL obj;
obj.push(5);
obj.push(60);
obj.push(4);
obj.push(9);
cout<<"Entered linked list is: "<<endl;
obj.print();
obj.reverse();
cout<<"Reversed linked list is: "<<endl;
obj.print();
return 0;
}
LL obj;
obj.push(5);
obj.push(60);
obj.push(4);
obj.push(9);

cout<<"Entered linked list is: "<<endl;
obj.print();

obj.reverse();

}
cout<<"\nReversed linked list is: "<<endl;
obj.print();
cout<<endl;

return 0;
}